From 65d5e2a47b417218bea151529639190d01a04c0b Mon Sep 17 00:00:00 2001 From: wackxu Date: Thu, 7 Dec 2017 20:41:26 +0800 Subject: [PATCH 1/4] Refactor crinit util package into multiple packages --- .idea/workspace.xml | 337 +++++++++++++ pkg/crinit/common/common.go | 456 ++++++++++++++++++ pkg/crinit/options/options.go | 391 +++++++++++++++ pkg/crinit/util/util.go | 868 ++-------------------------------- 4 files changed, 1216 insertions(+), 836 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 pkg/crinit/common/common.go create mode 100644 pkg/crinit/options/options.go diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..72eb0b42 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,337 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + createAPIServer + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + CSS + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pkg/crinit/aggregated/aggregated.go b/pkg/crinit/aggregated/aggregated.go index 740674d3..a61f973d 100644 --- a/pkg/crinit/aggregated/aggregated.go +++ b/pkg/crinit/aggregated/aggregated.go @@ -33,6 +33,8 @@ import ( "k8s.io/client-go/util/cert" "k8s.io/cluster-registry/pkg/apis/clusterregistry/v1alpha1" "k8s.io/cluster-registry/pkg/crinit/util" + "k8s.io/cluster-registry/pkg/crinit/options" + "k8s.io/cluster-registry/pkg/crinit/common" apiregv1beta1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1" apiregclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset" @@ -97,12 +99,12 @@ var ( ) type aggregatedClusterRegistryOptions struct { - util.SubcommandOptions + options.SubcommandOptions apiServerServiceTypeString string } func (o *aggregatedClusterRegistryOptions) Bind(flags *pflag.FlagSet) { - flags.StringVar(&o.apiServerServiceTypeString, util.APIServerServiceTypeFlag, + flags.StringVar(&o.apiServerServiceTypeString, options.APIServerServiceTypeFlag, string(v1.ServiceTypeNodePort), "The type of service to create for the cluster registry. Options: 'LoadBalancer', 'NodePort'.") } @@ -299,7 +301,7 @@ func createServiceAccount(clientset client.Interface, ObjectMeta: metav1.ObjectMeta{ Name: serviceAccountName, Namespace: namespace, - Labels: util.ComponentLabel, + Labels: common.ComponentLabel, }, } @@ -324,7 +326,7 @@ func createClusterRole(clientset client.Interface, cr := &rbacv1.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ Name: clusterRoleName, - Labels: util.ComponentLabel, + Labels: common.ComponentLabel, }, Rules: []rbacv1.PolicyRule{rule}, } @@ -345,7 +347,7 @@ func createClusterRoleBindings(clientset client.Interface, // cluster role. crb, err := createClusterRoleBindingObject(clientset, apiServerCRBName, rbacv1.ServiceAccountKind, serviceAccountName, namespace, rbacv1.GroupName, - "ClusterRole", clusterRoleName, util.ComponentLabel, dryRun) + "ClusterRole", clusterRoleName, common.ComponentLabel, dryRun) if err != nil { glog.V(4).Infof("Failed to create cluster role binding %v: %v", crb, err) @@ -355,7 +357,7 @@ func createClusterRoleBindings(clientset client.Interface, // Create cluster role binding for the system:auth-delegator cluster role. crb, err = createClusterRoleBindingObject(clientset, authDelegatorCRBName, rbacv1.ServiceAccountKind, serviceAccountName, namespace, rbacv1.GroupName, - "ClusterRole", "system:auth-delegator", util.ComponentLabel, dryRun) + "ClusterRole", "system:auth-delegator", common.ComponentLabel, dryRun) if err != nil { glog.V(4).Infof("Failed to create cluster role binding %v: %v", crb, err) @@ -404,7 +406,7 @@ func createExtensionAPIServerAuthenticationRoleBinding(clientset client.Interfac rb := &rbacv1.RoleBinding{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Labels: util.ComponentLabel, + Labels: common.ComponentLabel, }, Subjects: []rbacv1.Subject{ { @@ -462,7 +464,7 @@ func createAPIServiceObject(clientset apiregclient.Interface, apiSvc := &apiregv1beta1.APIService{ ObjectMeta: metav1.ObjectMeta{ Name: apiServiceName, - Labels: util.ComponentLabel, + Labels: common.ComponentLabel, }, Spec: apiregv1beta1.APIServiceSpec{ Service: &apiregv1beta1.ServiceReference{ diff --git a/pkg/crinit/common/common.go b/pkg/crinit/common/common.go index 7bf83c5c..9f6a10ac 100644 --- a/pkg/crinit/common/common.go +++ b/pkg/crinit/common/common.go @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package common contains code shared between the subcommands of crinit. package common import ( @@ -63,7 +64,7 @@ var ( ) type EntityKeyPairs struct { - Ca *triple.KeyPair + CA *triple.KeyPair Server *triple.KeyPair Admin *triple.KeyPair } @@ -182,10 +183,10 @@ func GetClusterNodeIPs(clientset client.Interface) ([]string, error) { // CreateAPIServerCredentialsSecret helper to create secret object and return // the object. func CreateAPIServerCredentialsSecret(clientset client.Interface, namespace, -credentialsName string, credentials Credentials, dryRun bool) (*v1.Secret, error) { +credentialsName string, credentials *Credentials, dryRun bool) (*v1.Secret, error) { // Build the secret object with API server credentials. data := map[string][]byte{ - "ca.crt": certutil.EncodeCertPEM(credentials.CertEntKeyPairs.Ca.Cert), + "ca.crt": certutil.EncodeCertPEM(credentials.CertEntKeyPairs.CA.Cert), "server.crt": certutil.EncodeCertPEM(credentials.CertEntKeyPairs.Server.Cert), "server.key": certutil.EncodePrivateKeyPEM(credentials.CertEntKeyPairs.Server.Key), } @@ -251,7 +252,7 @@ etcdPVStorageClass string, dryRun bool) (*v1.PersistentVolumeClaim, error) { return clientset.CoreV1().PersistentVolumeClaims(namespace).Create(pvc) } -// createAPIServer helper to create the apiserver deployment object and +// CreateAPIServer helper to create the apiserver deployment object and // return the object. func CreateAPIServer(clientset client.Interface, namespace, name, serverImage, etcdImage, advertiseAddress, credentialsName, serviceAccountName string, hasHTTPBasicAuthFile, diff --git a/pkg/crinit/options/options.go b/pkg/crinit/options/options.go index 07e5c73b..359893cc 100644 --- a/pkg/crinit/options/options.go +++ b/pkg/crinit/options/options.go @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package options contains implemention for SubcommandOptions. package options import ( @@ -42,7 +43,7 @@ const ( APIServerNameSuffix = "apiserver" CredentialSuffix = "credentials" - apiserverAdvertiseAddressFlag = "api-server-advertise-address" + APIServerAdvertiseAddressFlag = "api-server-advertise-address" APIServerServiceTypeFlag = "api-server-service-type" apiserverPortFlag = "api-server-port" ) @@ -65,12 +66,12 @@ type SubcommandOptions struct { EtcdPVStorageClass string EtcdPersistentStorage bool DryRun bool - ApiServerOverridesString string - ApiServerOverrides map[string]string - ApiServerServiceType v1.ServiceType - ApiServerAdvertiseAddress string - ApiServerNodePortPort int32 - ApiServerNodePortPortPtr *int32 + APIServerOverridesString string + APIServerOverrides map[string]string + APIServerServiceType v1.ServiceType + APIServerAdvertiseAddress string + APIServerNodePortPort int32 + APIServerNodePortPortPtr *int32 } // BindCommon adds the common options that are shared by different @@ -95,11 +96,11 @@ func (o *SubcommandOptions) BindCommon(flags *pflag.FlagSet, defaultServerImage, "Use a persistent volume for etcd. Defaults to 'true'.") flags.BoolVar(&o.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.") - flags.StringVar(&o.ApiServerOverridesString, "apiserver-arg-overrides", "", + flags.StringVar(&o.APIServerOverridesString, "apiserver-arg-overrides", "", "Comma-separated list of cluster registry API server arguments to override, e.g., \"--arg1=value1,--arg2=value2...\"") - flags.StringVar(&o.ApiServerAdvertiseAddress, apiserverAdvertiseAddressFlag, "", + flags.StringVar(&o.APIServerAdvertiseAddress, APIServerAdvertiseAddressFlag, "", "Preferred address at which to advertise the cluster registry API server NodePort service. Valid only if '"+APIServerServiceTypeFlag+"=NodePort'.") - flags.Int32Var(&o.ApiServerNodePortPort, apiserverPortFlag, 0, + flags.Int32Var(&o.APIServerNodePortPort, apiserverPortFlag, 0, "Preferred port to use for the cluster registry API server NodePort service. Set to 0 to randomly assign a port. Valid only if '"+APIServerServiceTypeFlag+"=NodePort'.") } @@ -118,36 +119,36 @@ func (o *SubcommandOptions) ValidateCommonOptions() error { serverName = fmt.Sprintf("%s-%s", o.Name, APIServerNameSuffix) serverCredName = fmt.Sprintf("%s-%s", serverName, CredentialSuffix) - if o.ApiServerServiceType != v1.ServiceTypeLoadBalancer && - o.ApiServerServiceType != v1.ServiceTypeNodePort { + if o.APIServerServiceType != v1.ServiceTypeLoadBalancer && + o.APIServerServiceType != v1.ServiceTypeNodePort { return fmt.Errorf("invalid %s: %s, should be either %s or %s", - APIServerServiceTypeFlag, o.ApiServerServiceType, + APIServerServiceTypeFlag, o.APIServerServiceType, v1.ServiceTypeLoadBalancer, v1.ServiceTypeNodePort) } - if o.ApiServerAdvertiseAddress != "" { - ip := net.ParseIP(o.ApiServerAdvertiseAddress) + if o.APIServerAdvertiseAddress != "" { + ip := net.ParseIP(o.APIServerAdvertiseAddress) if ip == nil { return fmt.Errorf("invalid %s: %s, should be a valid ip address", - apiserverAdvertiseAddressFlag, o.ApiServerAdvertiseAddress) + APIServerAdvertiseAddressFlag, o.APIServerAdvertiseAddress) } - if o.ApiServerServiceType != v1.ServiceTypeNodePort { + if o.APIServerServiceType != v1.ServiceTypeNodePort { return fmt.Errorf("%s should be passed only with '%s=NodePort'", - apiserverAdvertiseAddressFlag, APIServerServiceTypeFlag) + APIServerAdvertiseAddressFlag, APIServerServiceTypeFlag) } } - if o.ApiServerNodePortPort != 0 { - if o.ApiServerServiceType != v1.ServiceTypeNodePort { + if o.APIServerNodePortPort != 0 { + if o.APIServerServiceType != v1.ServiceTypeNodePort { return fmt.Errorf("%s should be passed only with '%s=NodePort'", apiserverPortFlag, APIServerServiceTypeFlag) } - o.ApiServerNodePortPortPtr = &o.ApiServerNodePortPort + o.APIServerNodePortPortPtr = &o.APIServerNodePortPort } else { - o.ApiServerNodePortPortPtr = nil + o.APIServerNodePortPortPtr = nil } - if o.ApiServerNodePortPort < 0 || o.ApiServerNodePortPort > 65535 { + if o.APIServerNodePortPort < 0 || o.APIServerNodePortPort > 65535 { return fmt.Errorf("Please provide a valid port number for %s", apiserverPortFlag) } @@ -156,12 +157,12 @@ func (o *SubcommandOptions) ValidateCommonOptions() error { // marshalOptions marshals options if necessary. func (o *SubcommandOptions) MarshalOptions() error { - if o.ApiServerOverridesString == "" { + if o.APIServerOverridesString == "" { return nil } argsMap := make(map[string]string) - overrideArgs := strings.Split(o.ApiServerOverridesString, ",") + overrideArgs := strings.Split(o.APIServerOverridesString, ",") for _, overrideArg := range overrideArgs { splitArg := strings.SplitN(overrideArg, "=", 2) if len(splitArg) != 2 { @@ -175,7 +176,7 @@ func (o *SubcommandOptions) MarshalOptions() error { argsMap[key] = val } - o.ApiServerOverrides = argsMap + o.APIServerOverrides = argsMap return nil } @@ -208,8 +209,8 @@ func (o *SubcommandOptions) CreateService(cmdOut io.Writer, glog.V(4).Info("Creating cluster registry API server service") svc, ips, hostnames, err := common.CreateService(cmdOut, clientset, - o.ClusterRegistryNamespace, o.Name, o.ApiServerAdvertiseAddress, - o.ApiServerNodePortPortPtr, o.ApiServerServiceType, o.DryRun) + o.ClusterRegistryNamespace, o.Name, o.APIServerAdvertiseAddress, + o.APIServerNodePortPortPtr, o.APIServerServiceType, o.DryRun) if err != nil { return nil, nil, nil, err @@ -245,7 +246,7 @@ func (o *SubcommandOptions) GenerateCredentials(cmdOut io.Writer, svcName string // CreateAPIServerCredentialsSecret creates the secret containing the // apiserver credentials passed in. func (o *SubcommandOptions) CreateAPIServerCredentialsSecret(clientset client.Interface, - credentials common.Credentials) error { + credentials *common.Credentials) error { _, err := common.CreateAPIServerCredentialsSecret(clientset, o.ClusterRegistryNamespace, serverCredName, credentials, o.DryRun) @@ -285,8 +286,8 @@ func (o *SubcommandOptions) CreateAPIServer(cmdOut io.Writer, clientset client.I pvc *v1.PersistentVolumeClaim, serviceAccountName string) error { // Since only one IP address can be specified as advertise address, // we arbitrarily pick the first available IP address. - // Pick user provided apiserverAdvertiseAddress over other available IP addresses. - advertiseAddress := o.ApiServerAdvertiseAddress + // Pick user provided APIServerAdvertiseAddress over other available IP addresses. + advertiseAddress := o.APIServerAdvertiseAddress if advertiseAddress == "" && len(ips) > 0 { advertiseAddress = ips[0] } @@ -297,7 +298,7 @@ func (o *SubcommandOptions) CreateAPIServer(cmdOut io.Writer, clientset client.I _, err := common.CreateAPIServer(clientset, o.ClusterRegistryNamespace, serverName, o.ServerImage, o.EtcdImage, advertiseAddress, serverCredName, serviceAccountName, apiServerEnableHTTPBasicAuth, apiServerEnableTokenAuth, - o.ApiServerOverrides, pvc, aggregated, o.DryRun) + o.APIServerOverrides, pvc, aggregated, o.DryRun) if err != nil { glog.V(4).Infof("Failed to create API server: %v", err) @@ -331,7 +332,7 @@ func (o *SubcommandOptions) UpdateKubeconfig(cmdOut io.Writer, // If the service is nodeport, need to append the port to endpoint as it is // non-standard port. - if o.ApiServerServiceType == v1.ServiceTypeNodePort { + if o.APIServerServiceType == v1.ServiceTypeNodePort { endpoint = endpoint + ":" + strconv.Itoa(int(svc.Spec.Ports[0].NodePort)) } diff --git a/pkg/crinit/standalone/standalone.go b/pkg/crinit/standalone/standalone.go index a7b67d15..305ddc3f 100644 --- a/pkg/crinit/standalone/standalone.go +++ b/pkg/crinit/standalone/standalone.go @@ -26,6 +26,7 @@ import ( client "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "k8s.io/cluster-registry/pkg/crinit/util" + "k8s.io/cluster-registry/pkg/crinit/options" "github.com/golang/glog" "github.com/spf13/cobra" @@ -48,14 +49,14 @@ var ( ) type standaloneClusterRegistryOptions struct { - util.SubcommandOptions + options.SubcommandOptions apiServerServiceTypeString string apiServerEnableHTTPBasicAuth bool apiServerEnableTokenAuth bool } func (o *standaloneClusterRegistryOptions) Bind(flags *pflag.FlagSet) { - flags.StringVar(&o.apiServerServiceTypeString, util.APIServerServiceTypeFlag, + flags.StringVar(&o.apiServerServiceTypeString, options.APIServerServiceTypeFlag, string(v1.ServiceTypeLoadBalancer), "The type of service to create for the cluster registry. Options: 'LoadBalancer', 'NodePort'.") flags.BoolVar(&o.apiServerEnableHTTPBasicAuth, "apiserver-enable-basic-auth", false, diff --git a/pkg/crinit/standalone/standalone_test.go b/pkg/crinit/standalone/standalone_test.go index c6b417c3..1df68277 100644 --- a/pkg/crinit/standalone/standalone_test.go +++ b/pkg/crinit/standalone/standalone_test.go @@ -28,7 +28,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" clientgotesting "k8s.io/client-go/testing" - "k8s.io/cluster-registry/pkg/crinit/util" + "k8s.io/cluster-registry/pkg/crinit/options" + "k8s.io/cluster-registry/pkg/crinit/common" ) func TestValidateOptions(t *testing.T) { @@ -45,7 +46,7 @@ func TestValidateOptions(t *testing.T) { initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeLoadBalancer)}, finalOpts: &standaloneClusterRegistryOptions{ - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerServiceType: v1.ServiceTypeLoadBalancer}, apiServerServiceTypeString: string(v1.ServiceTypeLoadBalancer)}, errExpected: false, @@ -54,7 +55,7 @@ func TestValidateOptions(t *testing.T) { desc: "NodePort service type supported", initialOpts: &standaloneClusterRegistryOptions{apiServerServiceTypeString: string(v1.ServiceTypeNodePort)}, finalOpts: &standaloneClusterRegistryOptions{ - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerServiceType: v1.ServiceTypeNodePort}, apiServerServiceTypeString: string(v1.ServiceTypeNodePort)}, errExpected: false, @@ -68,7 +69,7 @@ func TestValidateOptions(t *testing.T) { desc: "advertise address supported with NodePort service type", initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeNodePort), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerAdvertiseAddress: "10.0.0.1"}}, errExpected: false, }, @@ -76,7 +77,7 @@ func TestValidateOptions(t *testing.T) { desc: "advertise address not supported with non-NodePort service type", initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeLoadBalancer), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerAdvertiseAddress: "10.0.0.1"}}, errExpected: true, }, @@ -84,7 +85,7 @@ func TestValidateOptions(t *testing.T) { desc: "advertise address validated", initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeNodePort), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerAdvertiseAddress: "notAValidIP"}}, errExpected: true, }, @@ -92,11 +93,11 @@ func TestValidateOptions(t *testing.T) { desc: "advertise port supported with NodePort service type", initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeNodePort), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerNodePortPort: testNodePort}}, finalOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeNodePort), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerServiceType: v1.ServiceTypeNodePort, APIServerNodePortPort: testNodePort, APIServerNodePortPortPtr: &testNodePort}}, @@ -106,7 +107,7 @@ func TestValidateOptions(t *testing.T) { desc: "advertise port rejected with non-NodePort service type", initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeLoadBalancer), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerNodePortPort: testNodePort}}, errExpected: true, }, @@ -114,7 +115,7 @@ func TestValidateOptions(t *testing.T) { desc: "advertise port rejected if out of range", initialOpts: &standaloneClusterRegistryOptions{ apiServerServiceTypeString: string(v1.ServiceTypeNodePort), - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerNodePortPort: 100000}}, errExpected: true, }, @@ -187,7 +188,7 @@ func TestMarshalOptions(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { options := standaloneClusterRegistryOptions{ - SubcommandOptions: util.SubcommandOptions{ + SubcommandOptions: options.SubcommandOptions{ APIServerOverridesString: tc.overrideParams}} err := options.MarshalOptions() if tc.expectedErr == "" { @@ -210,7 +211,7 @@ func TestCreateNamespace(t *testing.T) { t.Run("simple namespace creation", func(t *testing.T) { name := "test" client := fake.NewSimpleClientset() - ns, err := util.CreateNamespace(client, name, false) + ns, err := common.CreateNamespace(client, name, false) if ns == nil { t.Error("namespace not created") } @@ -228,7 +229,7 @@ func TestCreateNamespace(t *testing.T) { t.Run("dry run should not create namespace on server", func(t *testing.T) { name := "test2" client := fake.NewSimpleClientset() - ns, _ := util.CreateNamespace(client, name, true) + ns, _ := common.CreateNamespace(client, name, true) if ns == nil { t.Error("namespace not returned") } @@ -246,7 +247,7 @@ func TestCreateNamespace(t *testing.T) { client.AddReactor("create", "namespaces", func(action clientgotesting.Action) (bool, runtime.Object, error) { return true, nil, errors.New("error") }) - ns, err := util.CreateNamespace(client, name, false) + ns, err := common.CreateNamespace(client, name, false) if err == nil { t.Error("expected error, got none") } @@ -261,7 +262,7 @@ func TestCreateService(t *testing.T) { name := "test" client := fake.NewSimpleClientset() buffer := &bytes.Buffer{} - util.CreateService(buffer, client, "ns", name, "", nil, v1.ServiceTypeClusterIP, false) + common.CreateService(buffer, client, "ns", name, "", nil, v1.ServiceTypeClusterIP, false) if serverSvc, _ := client.CoreV1().Services("ns").Get(name, metav1.GetOptions{}); serverSvc == nil { t.Error("should create service") } @@ -271,7 +272,7 @@ func TestCreateService(t *testing.T) { name := "test" client := fake.NewSimpleClientset() buffer := &bytes.Buffer{} - svc, ips, hostnames, err := util.CreateService(buffer, client, "ns", name, "", nil, v1.ServiceTypeClusterIP, true) + svc, ips, hostnames, err := common.CreateService(buffer, client, "ns", name, "", nil, v1.ServiceTypeClusterIP, true) if svc == nil { t.Error("service not returned") } @@ -360,7 +361,7 @@ func TestCreateService(t *testing.T) { }) buffer := &bytes.Buffer{} - svc, ips, hostnames, _ := util.CreateService(buffer, client, tc.namespace, tc.name, tc.advertiseAddress, &tc.advertisePort, tc.serviceType, false) + svc, ips, hostnames, _ := common.CreateService(buffer, client, tc.namespace, tc.name, tc.advertiseAddress, &tc.advertisePort, tc.serviceType, false) if svc == nil { t.Error("service not returned") @@ -410,7 +411,7 @@ func TestCreateService(t *testing.T) { return true, nil, errors.New("error") }) buffer := &bytes.Buffer{} - svc, ips, hostnames, err := util.CreateService(buffer, client, "ns", "test", "", nil, tc.serviceType, false) + svc, ips, hostnames, err := common.CreateService(buffer, client, "ns", "test", "", nil, tc.serviceType, false) if err == nil { t.Error("Expected error, got none") } @@ -503,7 +504,7 @@ func TestGetClusterNodeIPs(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { client := fake.NewSimpleClientset(tc.nodes...) - got, _ := util.GetClusterNodeIPs(client) + got, _ := common.GetClusterNodeIPs(client) want := tc.wantedIPs sort.Strings(got) sort.Strings(want) diff --git a/pkg/crinit/util/util.go b/pkg/crinit/util/util.go index bb409c17..18a5dac4 100644 --- a/pkg/crinit/util/util.go +++ b/pkg/crinit/util/util.go @@ -77,7 +77,7 @@ func GenCerts(svcNamespace, name, svcName, localDNSZoneName string, return nil, fmt.Errorf("failed to create client key and certificate for an admin: %v", err) } return &common.EntityKeyPairs{ - Ca: ca, + CA: ca, Server: server, Admin: admin, }, nil @@ -116,7 +116,7 @@ kubeConfigPath string, credentials *common.Credentials, dryRun bool) error { } cluster.Server = endpoint - cluster.CertificateAuthorityData = certutil.EncodeCertPEM(credentials.CertEntKeyPairs.Ca.Cert) + cluster.CertificateAuthorityData = certutil.EncodeCertPEM(credentials.CertEntKeyPairs.CA.Cert) // Populate credentials. authInfo := clientcmdapi.NewAuthInfo() @@ -199,5 +199,5 @@ func GetCAKeyPair(credentials *common.Credentials) *triple.KeyPair { return nil } - return credentials.CertEntKeyPairs.Ca + return credentials.CertEntKeyPairs.CA } From cfa52a1cbeb80c6b979a8deb38d72b0609648c38 Mon Sep 17 00:00:00 2001 From: wackxu Date: Wed, 10 Jan 2018 17:14:53 +0800 Subject: [PATCH 3/4] fix cycle in the dependency --- pkg/crinit/common/common.go | 14 +------------- pkg/crinit/options/options.go | 6 +++--- pkg/crinit/util/util.go | 26 +++++++++++++++++++------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/pkg/crinit/common/common.go b/pkg/crinit/common/common.go index 9f6a10ac..075234ca 100644 --- a/pkg/crinit/common/common.go +++ b/pkg/crinit/common/common.go @@ -33,7 +33,6 @@ import ( certutil "k8s.io/client-go/util/cert" "k8s.io/cluster-registry/pkg/crinit/util" - "k8s.io/client-go/util/cert/triple" ) const ( @@ -63,18 +62,7 @@ var ( } ) -type EntityKeyPairs struct { - CA *triple.KeyPair - Server *triple.KeyPair - Admin *triple.KeyPair -} -type Credentials struct { - Username string - Password string - Token string - CertEntKeyPairs *EntityKeyPairs -} // CreateNamespace helper to create the cluster registry namespace object and return // the object. @@ -183,7 +171,7 @@ func GetClusterNodeIPs(clientset client.Interface) ([]string, error) { // CreateAPIServerCredentialsSecret helper to create secret object and return // the object. func CreateAPIServerCredentialsSecret(clientset client.Interface, namespace, -credentialsName string, credentials *Credentials, dryRun bool) (*v1.Secret, error) { +credentialsName string, credentials *util.Credentials, dryRun bool) (*v1.Secret, error) { // Build the secret object with API server credentials. data := map[string][]byte{ "ca.crt": certutil.EncodeCertPEM(credentials.CertEntKeyPairs.CA.Cert), diff --git a/pkg/crinit/options/options.go b/pkg/crinit/options/options.go index 359893cc..fd4d7f51 100644 --- a/pkg/crinit/options/options.go +++ b/pkg/crinit/options/options.go @@ -226,7 +226,7 @@ func (o *SubcommandOptions) CreateService(cmdOut io.Writer, // GenerateCredentials creates the credentials for apiserver secret. func (o *SubcommandOptions) GenerateCredentials(cmdOut io.Writer, svcName string, ips, hostnames []string, apiServerEnableHTTPBasicAuth, - apiServerEnableTokenAuth bool) (*common.Credentials, error) { + apiServerEnableTokenAuth bool) (*util.Credentials, error) { fmt.Fprint(cmdOut, "Creating cluster registry objects (credentials, persistent volume claim)...") @@ -246,7 +246,7 @@ func (o *SubcommandOptions) GenerateCredentials(cmdOut io.Writer, svcName string // CreateAPIServerCredentialsSecret creates the secret containing the // apiserver credentials passed in. func (o *SubcommandOptions) CreateAPIServerCredentialsSecret(clientset client.Interface, - credentials *common.Credentials) error { + credentials *util.Credentials) error { _, err := common.CreateAPIServerCredentialsSecret(clientset, o.ClusterRegistryNamespace, serverCredName, credentials, o.DryRun) @@ -315,7 +315,7 @@ func (o *SubcommandOptions) CreateAPIServer(cmdOut io.Writer, clientset client.I // while printing and logging progress. func (o *SubcommandOptions) UpdateKubeconfig(cmdOut io.Writer, pathOptions *clientcmd.PathOptions, svc *v1.Service, ips, hostnames []string, - credentials *common.Credentials) error { + credentials *util.Credentials) error { fmt.Fprint(cmdOut, "Updating kubeconfig...") glog.V(4).Info("Updating kubeconfig") diff --git a/pkg/crinit/util/util.go b/pkg/crinit/util/util.go index 18a5dac4..33e188f6 100644 --- a/pkg/crinit/util/util.go +++ b/pkg/crinit/util/util.go @@ -30,7 +30,6 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" certutil "k8s.io/client-go/util/cert" "k8s.io/client-go/util/cert/triple" - "k8s.io/cluster-registry/pkg/crinit/common" "github.com/golang/glog" ) @@ -39,11 +38,24 @@ const ( AdminCN = "admin" ) +type EntityKeyPairs struct { + CA *triple.KeyPair + Server *triple.KeyPair + Admin *triple.KeyPair +} + +type Credentials struct { + Username string + Password string + Token string + CertEntKeyPairs *EntityKeyPairs +} + // generateCredentials helper to create the certs for the apiserver. func GenerateCredentials(svcNamespace, name, svcName, localDNSZoneName string, - ips, hostnames []string, enableHTTPBasicAuth, enableTokenAuth bool) (*common.Credentials, error) { + ips, hostnames []string, enableHTTPBasicAuth, enableTokenAuth bool) (*Credentials, error) { - credentials := common.Credentials{ + credentials := Credentials{ Username: AdminCN, } if enableHTTPBasicAuth { @@ -62,7 +74,7 @@ func GenerateCredentials(svcNamespace, name, svcName, localDNSZoneName string, } func GenCerts(svcNamespace, name, svcName, localDNSZoneName string, - ips, hostnames []string) (*common.EntityKeyPairs, error) { + ips, hostnames []string) (*EntityKeyPairs, error) { ca, err := triple.NewCA(name) if err != nil { @@ -76,7 +88,7 @@ func GenCerts(svcNamespace, name, svcName, localDNSZoneName string, if err != nil { return nil, fmt.Errorf("failed to create client key and certificate for an admin: %v", err) } - return &common.EntityKeyPairs{ + return &EntityKeyPairs{ CA: ca, Server: server, Admin: admin, @@ -99,7 +111,7 @@ func ArgMapsToArgStrings(argsMap, overrides map[string]string) []string { // UpdateKubeconfig helper to update the kubeconfig file based on input // parameters. func UpdateKubeconfig(pathOptions *clientcmd.PathOptions, name, endpoint, -kubeConfigPath string, credentials *common.Credentials, dryRun bool) error { +kubeConfigPath string, credentials *Credentials, dryRun bool) error { pathOptions.LoadingRules.ExplicitPath = kubeConfigPath kubeconfig, err := pathOptions.GetStartingConfig() @@ -193,7 +205,7 @@ func AuthFileContents(username, authSecret string) []byte { // GetCAKeyPair retrieves the CA key pair stored in the internal credentials // structure. -func GetCAKeyPair(credentials *common.Credentials) *triple.KeyPair { +func GetCAKeyPair(credentials *Credentials) *triple.KeyPair { if credentials == nil { glog.V(4).Info("credentials argument is nil!") return nil From 7e6530b589e8122af345078f634fe26a8d6c548f Mon Sep 17 00:00:00 2001 From: wackxu Date: Thu, 11 Jan 2018 09:39:08 +0800 Subject: [PATCH 4/4] update bazel --- pkg/crinit/aggregated/BUILD.bazel | 2 ++ pkg/crinit/common/BUILD.bazel | 19 +++++++++++++++++++ pkg/crinit/options/BUILD.bazel | 17 +++++++++++++++++ pkg/crinit/standalone/BUILD.bazel | 4 +++- pkg/crinit/util/BUILD.bazel | 7 ------- 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 pkg/crinit/common/BUILD.bazel create mode 100644 pkg/crinit/options/BUILD.bazel diff --git a/pkg/crinit/aggregated/BUILD.bazel b/pkg/crinit/aggregated/BUILD.bazel index 60755147..b68dc700 100644 --- a/pkg/crinit/aggregated/BUILD.bazel +++ b/pkg/crinit/aggregated/BUILD.bazel @@ -7,6 +7,8 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/apis/clusterregistry/v1alpha1:go_default_library", + "//pkg/crinit/common:go_default_library", + "//pkg/crinit/options:go_default_library", "//pkg/crinit/util:go_default_library", "//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library", diff --git a/pkg/crinit/common/BUILD.bazel b/pkg/crinit/common/BUILD.bazel new file mode 100644 index 00000000..a1b764d0 --- /dev/null +++ b/pkg/crinit/common/BUILD.bazel @@ -0,0 +1,19 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["common.go"], + importpath = "k8s.io/cluster-registry/pkg/crinit/common", + visibility = ["//visibility:public"], + deps = [ + "//pkg/crinit/util:go_default_library", + "//vendor/k8s.io/api/apps/v1beta1:go_default_library", + "//vendor/k8s.io/api/core/v1:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library", + "//vendor/k8s.io/client-go/kubernetes:go_default_library", + "//vendor/k8s.io/client-go/util/cert:go_default_library", + ], +) diff --git a/pkg/crinit/options/BUILD.bazel b/pkg/crinit/options/BUILD.bazel new file mode 100644 index 00000000..f1a0fc3d --- /dev/null +++ b/pkg/crinit/options/BUILD.bazel @@ -0,0 +1,17 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["options.go"], + importpath = "k8s.io/cluster-registry/pkg/crinit/options", + visibility = ["//visibility:public"], + deps = [ + "//pkg/crinit/common:go_default_library", + "//pkg/crinit/util:go_default_library", + "//vendor/github.com/golang/glog:go_default_library", + "//vendor/github.com/spf13/pflag:go_default_library", + "//vendor/k8s.io/api/core/v1:go_default_library", + "//vendor/k8s.io/client-go/kubernetes:go_default_library", + "//vendor/k8s.io/client-go/tools/clientcmd:go_default_library", + ], +) diff --git a/pkg/crinit/standalone/BUILD.bazel b/pkg/crinit/standalone/BUILD.bazel index 592a42db..10110c94 100644 --- a/pkg/crinit/standalone/BUILD.bazel +++ b/pkg/crinit/standalone/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "k8s.io/cluster-registry/pkg/crinit/standalone", visibility = ["//visibility:public"], deps = [ + "//pkg/crinit/options:go_default_library", "//pkg/crinit/util:go_default_library", "//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library", @@ -22,7 +23,8 @@ go_test( importpath = "k8s.io/cluster-registry/pkg/crinit/standalone", library = ":go_default_library", deps = [ - "//pkg/crinit/util:go_default_library", + "//pkg/crinit/common:go_default_library", + "//pkg/crinit/options:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", diff --git a/pkg/crinit/util/BUILD.bazel b/pkg/crinit/util/BUILD.bazel index dc4116ed..49d25559 100644 --- a/pkg/crinit/util/BUILD.bazel +++ b/pkg/crinit/util/BUILD.bazel @@ -7,15 +7,8 @@ go_library( visibility = ["//visibility:public"], deps = [ "//vendor/github.com/golang/glog:go_default_library", - "//vendor/github.com/spf13/pflag:go_default_library", - "//vendor/k8s.io/api/apps/v1beta1:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library", - "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", - "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", - "//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/uuid:go_default_library", - "//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library", - "//vendor/k8s.io/client-go/kubernetes:go_default_library", "//vendor/k8s.io/client-go/tools/clientcmd:go_default_library", "//vendor/k8s.io/client-go/tools/clientcmd/api:go_default_library", "//vendor/k8s.io/client-go/util/cert:go_default_library",