This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from G-Harmon/mciList
kubemci list command: look for relevant global forwarding rules.
- Loading branch information
Showing
10 changed files
with
319 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2017 Google, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-multicluster-ingress/app/kubemci/pkg/gcp/cloudinterface" | ||
gcplb "github.com/GoogleCloudPlatform/k8s-multicluster-ingress/app/kubemci/pkg/gcp/loadbalancer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
listShortDesc = "List multicluster ingresses." | ||
listLongDesc = `List multicluster ingresses. | ||
List multicluster ingresses found in the given GCP project. Searches for MCIs based on naming convention. | ||
` | ||
) | ||
|
||
type listOptions struct { | ||
// Name of the GCP project in which the load balancer should be configured. | ||
// Required | ||
// TODO(nikhiljindal): This should be optional. Figure it out from gcloud settings. | ||
GCPProject string | ||
} | ||
|
||
func newCmdList(out, err io.Writer) *cobra.Command { | ||
var options listOptions | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: listShortDesc, | ||
Long: listLongDesc, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := validateListArgs(&options, args); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
if err := runList(&options, args); err != nil { | ||
fmt.Println("Error listing load balancers:", err) | ||
return | ||
} | ||
}, | ||
} | ||
addListFlags(cmd, &options) | ||
return cmd | ||
} | ||
|
||
func addListFlags(cmd *cobra.Command, options *listOptions) error { | ||
cmd.Flags().StringVarP(&options.GCPProject, "gcp-project", "", options.GCPProject, "[required] name of the gcp project") | ||
return nil | ||
} | ||
|
||
func validateListArgs(options *listOptions, args []string) error { | ||
// Verify that the required flag is not missing. | ||
if options.GCPProject == "" { | ||
return fmt.Errorf("unexpected missing argument gcp-project.") | ||
} | ||
if len(args) != 0 { | ||
return fmt.Errorf("unexpected args: %v. Expected 0 arguments.", args) | ||
} | ||
return nil | ||
} | ||
|
||
// runList prints a list of all mcis that we've created. | ||
func runList(options *listOptions, args []string) error { | ||
cloudInterface, err := cloudinterface.NewGCECloudInterface(options.GCPProject) | ||
if err != nil { | ||
fmt.Println("error creating cloud interface:", err) | ||
return fmt.Errorf("error in creating cloud interface: %s", err) | ||
} | ||
|
||
lbs, err := gcplb.NewLoadBalancerSyncer("" /*lbName*/, nil /* clientset */, cloudInterface, options.GCPProject) | ||
if err != nil { | ||
fmt.Println("Error creating load balancer syncer:", err) | ||
return err | ||
} | ||
balancers, err := lbs.ListLoadBalancers() | ||
if err != nil { | ||
fmt.Println("Error getting list of load balancers:", err) | ||
return err | ||
} | ||
fmt.Println(balancers) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2017 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestValidateListArgs(t *testing.T) { | ||
// It should return an error with extra args. | ||
options := listOptions{} | ||
if err := validateListArgs(&options, []string{"arg1"}); err == nil { | ||
t.Errorf("Expected error for non-empty args") | ||
} | ||
|
||
// It should return an error with missing project. | ||
options = listOptions{} | ||
if err := validateListArgs(&options, []string{}); err == nil { | ||
t.Errorf("Expected error for non-empty args") | ||
} | ||
|
||
// validateListArgs should succeed with a project and empty args. | ||
options = listOptions{ | ||
GCPProject: "myGcpProject", | ||
} | ||
if err := validateListArgs(&options, []string{}); err != nil { | ||
t.Errorf("unexpected error from validateListArgs: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.