From fe4828498e251bc51e2cecf910ab07858d04bbb2 Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Tue, 21 Mar 2023 15:50:45 -0500 Subject: [PATCH 1/4] Expand all kubectl calls to fully qualified names --- pkg/executables/kubectl.go | 68 ++++++++++---------------- pkg/executables/kubectl_test.go | 86 ++++++--------------------------- 2 files changed, 39 insertions(+), 115 deletions(-) diff --git a/pkg/executables/kubectl.go b/pkg/executables/kubectl.go index e929f12994e7..5fdd1ad37d7f 100644 --- a/pkg/executables/kubectl.go +++ b/pkg/executables/kubectl.go @@ -54,15 +54,21 @@ const ( var ( capiClustersResourceType = fmt.Sprintf("clusters.%s", clusterv1.GroupVersion.Group) + capiProvidersResourceType = fmt.Sprintf("providers.clusterctl.%s", clusterv1.GroupVersion.Group) + capiMachinesType = fmt.Sprintf("machines.%s", clusterv1.GroupVersion.Group) + capiMachineDeploymentsType = fmt.Sprintf("machinedeployments.%s", clusterv1.GroupVersion.Group) + capiMachineSetsType = fmt.Sprintf("machinesets.%s", clusterv1.GroupVersion.Group) eksaClusterResourceType = fmt.Sprintf("clusters.%s", v1alpha1.GroupVersion.Group) eksaVSphereDatacenterResourceType = fmt.Sprintf("vspheredatacenterconfigs.%s", v1alpha1.GroupVersion.Group) eksaVSphereMachineResourceType = fmt.Sprintf("vspheremachineconfigs.%s", v1alpha1.GroupVersion.Group) + vsphereMachineTemplatesType = fmt.Sprintf("vspheremachinetemplates.infrastructure.%s", clusterv1.GroupVersion.Group) eksaTinkerbellDatacenterResourceType = fmt.Sprintf("tinkerbelldatacenterconfigs.%s", v1alpha1.GroupVersion.Group) eksaTinkerbellMachineResourceType = fmt.Sprintf("tinkerbellmachineconfigs.%s", v1alpha1.GroupVersion.Group) TinkerbellHardwareResourceType = fmt.Sprintf("hardware.%s", tinkv1alpha1.GroupVersion.Group) rufioMachineResourceType = fmt.Sprintf("machines.%s", rufiov1alpha1.GroupVersion.Group) eksaCloudStackDatacenterResourceType = fmt.Sprintf("cloudstackdatacenterconfigs.%s", v1alpha1.GroupVersion.Group) eksaCloudStackMachineResourceType = fmt.Sprintf("cloudstackmachineconfigs.%s", v1alpha1.GroupVersion.Group) + cloudstackMachineTemplatesType = fmt.Sprintf("cloudstackmachinetemplates.infrastructure.%s", clusterv1.GroupVersion.Group) eksaNutanixDatacenterResourceType = fmt.Sprintf("nutanixdatacenterconfigs.%s", v1alpha1.GroupVersion.Group) eksaNutanixMachineResourceType = fmt.Sprintf("nutanixmachineconfigs.%s", v1alpha1.GroupVersion.Group) eksaAwsResourceType = fmt.Sprintf("awsdatacenterconfigs.%s", v1alpha1.GroupVersion.Group) @@ -76,7 +82,8 @@ var ( kubeadmControlPlaneResourceType = fmt.Sprintf("kubeadmcontrolplanes.controlplane.%s", clusterv1.GroupVersion.Group) eksdReleaseType = fmt.Sprintf("releases.%s", eksdv1alpha1.GroupVersion.Group) eksaPackagesType = fmt.Sprintf("packages.%s", packagesv1.GroupVersion.Group) - capiProvidersResourceType = fmt.Sprintf("providers.clusterctl.%s", clusterv1.GroupVersion.Group) + eksaPackagesBundleControllerType = fmt.Sprintf("packagebundlecontroller.%s", packagesv1.GroupVersion.Group) + eksaPackageBundlesType = fmt.Sprintf("packagebundles.%s", packagesv1.GroupVersion.Group) kubectlConnectionRefusedRegex = regexp.MustCompile("The connection to the server .* was refused") kubectlIoTimeoutRegex = regexp.MustCompile("Unable to connect to the server.*i/o timeout.*") ) @@ -85,31 +92,6 @@ type Kubectl struct { Executable } -type capiMachinesResponse struct { - Items []clusterv1.Machine -} - -// GetCAPIMachines returns all the CAPI machines for the provided clusterName. -func (k *Kubectl) GetCAPIMachines(ctx context.Context, cluster *types.Cluster, clusterName string) ([]clusterv1.Machine, error) { - params := []string{ - "get", "machines.cluster.x-k8s.io", "-o", "json", "--kubeconfig", cluster.KubeconfigFile, - "--selector=cluster.x-k8s.io/cluster-name=" + clusterName, - "--namespace", constants.EksaSystemNamespace, - } - stdOut, err := k.Execute(ctx, params...) - if err != nil { - return nil, fmt.Errorf("getting machines: %v", err) - } - - response := &capiMachinesResponse{} - err = json.Unmarshal(stdOut.Bytes(), response) - if err != nil { - return nil, fmt.Errorf("parsing get machines response: %v", err) - } - - return response.Items, nil -} - func (k *Kubectl) SearchCloudStackMachineConfig(ctx context.Context, name string, kubeconfigFile string, namespace string) ([]*v1alpha1.CloudStackMachineConfig, error) { params := []string{ "get", eksaCloudStackMachineResourceType, "-o", "json", "--kubeconfig", @@ -344,7 +326,7 @@ func (k *Kubectl) WaitForManagedExternalEtcdNotReady(ctx context.Context, cluste } func (k *Kubectl) WaitForMachineDeploymentReady(ctx context.Context, cluster *types.Cluster, timeout string, machineDeploymentName string) error { - return k.Wait(ctx, cluster.KubeconfigFile, timeout, "Ready=true", fmt.Sprintf("machinedeployments.%s/%s", clusterv1.GroupVersion.Group, machineDeploymentName), constants.EksaSystemNamespace) + return k.Wait(ctx, cluster.KubeconfigFile, timeout, "Ready=true", fmt.Sprintf("%s/%s", capiMachineDeploymentsType, machineDeploymentName), constants.EksaSystemNamespace) } // WaitForService blocks until an IP address is assigned. @@ -643,7 +625,7 @@ func (k *Kubectl) DeleteFluxConfig(ctx context.Context, managementCluster *types // GetPackageBundleController will retrieve the packagebundlecontroller from eksa-packages namespace and return the object. func (k *Kubectl) GetPackageBundleController(ctx context.Context, kubeconfigFile, clusterName string) (packagesv1.PackageBundleController, error) { - params := []string{"get", "pbc", clusterName, "-o", "json", "--kubeconfig", kubeconfigFile, "--namespace", "eksa-packages", "--ignore-not-found=true"} + params := []string{"get", eksaPackagesBundleControllerType, clusterName, "-o", "json", "--kubeconfig", kubeconfigFile, "--namespace", constants.EksaPackagesName, "--ignore-not-found=true"} stdOut, _ := k.Execute(ctx, params...) response := &packagesv1.PackageBundleController{} err := json.Unmarshal(stdOut.Bytes(), response) @@ -655,11 +637,11 @@ func (k *Kubectl) GetPackageBundleController(ctx context.Context, kubeconfigFile // GetPackageBundleList will retrieve the packagebundle list from eksa-packages namespace and return the list. func (k *Kubectl) GetPackageBundleList(ctx context.Context, kubeconfigFile string) ([]packagesv1.PackageBundle, error) { - err := k.WaitJSONPathLoop(ctx, kubeconfigFile, "5m", "items", "PackageBundle", "packagebundles", "eksa-packages") + err := k.WaitJSONPathLoop(ctx, kubeconfigFile, "5m", "items", "PackageBundle", eksaPackageBundlesType, constants.EksaPackagesName) if err != nil { return nil, fmt.Errorf("waiting on package bundle resource to exist %v", err) } - params := []string{"get", "packagebundle", "-o", "json", "--kubeconfig", kubeconfigFile, "--namespace", "eksa-packages", "--ignore-not-found=true"} + params := []string{"get", eksaPackageBundlesType, "-o", "json", "--kubeconfig", kubeconfigFile, "--namespace", constants.EksaPackagesName, "--ignore-not-found=true"} stdOut, err := k.Execute(ctx, params...) if err != nil { return nil, fmt.Errorf("getting package bundle resource %v", err) @@ -673,7 +655,7 @@ func (k *Kubectl) GetPackageBundleList(ctx context.Context, kubeconfigFile strin } func (k *Kubectl) DeletePackageResources(ctx context.Context, managementCluster *types.Cluster, clusterName string) error { - params := []string{"delete", "pbc", clusterName, "--kubeconfig", managementCluster.KubeconfigFile, "--namespace", "eksa-packages", "--ignore-not-found=true"} + params := []string{"delete", eksaPackagesBundleControllerType, clusterName, "--kubeconfig", managementCluster.KubeconfigFile, "--namespace", constants.EksaPackagesName, "--ignore-not-found=true"} _, err := k.Execute(ctx, params...) if err != nil { return fmt.Errorf("deleting package resources for %s: %v", clusterName, err) @@ -877,7 +859,7 @@ func (k *Kubectl) VsphereWorkerNodesMachineTemplate(ctx context.Context, cluster return nil, err } - params := []string{"get", "vspheremachinetemplates", machineTemplateName, "-o", "go-template", "--template", "{{.spec.template.spec}}", "-o", "yaml", "--kubeconfig", kubeconfig, "--namespace", namespace} + params := []string{"get", vsphereMachineTemplatesType, machineTemplateName, "-o", "go-template", "--template", "{{.spec.template.spec}}", "-o", "yaml", "--kubeconfig", kubeconfig, "--namespace", namespace} buffer, err := k.Execute(ctx, params...) if err != nil { return nil, err @@ -895,7 +877,7 @@ func (k *Kubectl) CloudstackWorkerNodesMachineTemplate(ctx context.Context, clus return nil, err } - params := []string{"get", "cloudstackmachinetemplates", machineTemplateName, "-o", "go-template", "--template", "{{.spec.template.spec}}", "-o", "yaml", "--kubeconfig", kubeconfig, "--namespace", namespace} + params := []string{"get", cloudstackMachineTemplatesType, machineTemplateName, "-o", "go-template", "--template", "{{.spec.template.spec}}", "-o", "yaml", "--kubeconfig", kubeconfig, "--namespace", namespace} buffer, err := k.Execute(ctx, params...) if err != nil { return nil, err @@ -909,7 +891,7 @@ func (k *Kubectl) CloudstackWorkerNodesMachineTemplate(ctx context.Context, clus func (k *Kubectl) MachineTemplateName(ctx context.Context, clusterName string, kubeconfig string, opts ...KubectlOpt) (string, error) { template := "{{.spec.template.spec.infrastructureRef.name}}" - params := []string{"get", "MachineDeployment", fmt.Sprintf("%s-md-0", clusterName), "-o", "go-template", "--template", template, "--kubeconfig", kubeconfig} + params := []string{"get", capiMachineDeploymentsType, fmt.Sprintf("%s-md-0", clusterName), "-o", "go-template", "--template", template, "--kubeconfig", kubeconfig} applyOpts(¶ms, opts...) buffer, err := k.Execute(ctx, params...) if err != nil { @@ -1031,7 +1013,7 @@ type machinesResponse struct { func (k *Kubectl) GetMachines(ctx context.Context, cluster *types.Cluster, clusterName string) ([]types.Machine, error) { params := []string{ - "get", "machines.cluster.x-k8s.io", "-o", "json", "--kubeconfig", cluster.KubeconfigFile, + "get", capiMachinesType, "-o", "json", "--kubeconfig", cluster.KubeconfigFile, "--selector=cluster.x-k8s.io/cluster-name=" + clusterName, "--namespace", constants.EksaSystemNamespace, } @@ -1055,7 +1037,7 @@ type machineSetResponse struct { func (k *Kubectl) GetMachineSets(ctx context.Context, machineDeploymentName string, cluster *types.Cluster) ([]clusterv1.MachineSet, error) { params := []string{ - "get", "machinesets", "-o", "json", "--kubeconfig", cluster.KubeconfigFile, + "get", capiMachineSetsType, "-o", "json", "--kubeconfig", cluster.KubeconfigFile, "--selector=cluster.x-k8s.io/deployment-name=" + machineDeploymentName, "--namespace", constants.EksaSystemNamespace, } @@ -1121,7 +1103,7 @@ type NutanixMachineConfigResponse struct { } func (k *Kubectl) ValidateClustersCRD(ctx context.Context, cluster *types.Cluster) error { - params := []string{"get", "crd", capiClustersResourceType, "--kubeconfig", cluster.KubeconfigFile} + params := []string{"get", "customresourcedefinition", capiClustersResourceType, "--kubeconfig", cluster.KubeconfigFile} _, err := k.Execute(ctx, params...) if err != nil { return fmt.Errorf("getting clusters crd: %v", err) @@ -1130,7 +1112,7 @@ func (k *Kubectl) ValidateClustersCRD(ctx context.Context, cluster *types.Cluste } func (k *Kubectl) ValidateEKSAClustersCRD(ctx context.Context, cluster *types.Cluster) error { - params := []string{"get", "crd", eksaClusterResourceType, "--kubeconfig", cluster.KubeconfigFile} + params := []string{"get", "customresourcedefinition", eksaClusterResourceType, "--kubeconfig", cluster.KubeconfigFile} _, err := k.Execute(ctx, params...) if err != nil { return fmt.Errorf("getting eksa clusters crd: %v", err) @@ -1140,7 +1122,7 @@ func (k *Kubectl) ValidateEKSAClustersCRD(ctx context.Context, cluster *types.Cl func (k *Kubectl) RolloutRestartDaemonSet(ctx context.Context, dsName, dsNamespace, kubeconfig string) error { params := []string{ - "rollout", "restart", "ds", dsName, + "rollout", "restart", "daemonset", dsName, "--kubeconfig", kubeconfig, "--namespace", dsNamespace, } _, err := k.Execute(ctx, params...) @@ -1369,7 +1351,7 @@ func (k *Kubectl) GetKubeadmControlPlane(ctx context.Context, cluster *types.Clu } func (k *Kubectl) GetMachineDeployment(ctx context.Context, workerNodeGroupName string, opts ...KubectlOpt) (*clusterv1.MachineDeployment, error) { - params := []string{"get", fmt.Sprintf("machinedeployments.%s", clusterv1.GroupVersion.Group), workerNodeGroupName, "-o", "json"} + params := []string{"get", capiMachineDeploymentsType, workerNodeGroupName, "-o", "json"} applyOpts(¶ms, opts...) stdOut, err := k.Execute(ctx, params...) if err != nil { @@ -1387,7 +1369,7 @@ func (k *Kubectl) GetMachineDeployment(ctx context.Context, workerNodeGroupName // GetMachineDeployments retrieves all Machine Deployments. func (k *Kubectl) GetMachineDeployments(ctx context.Context, opts ...KubectlOpt) ([]clusterv1.MachineDeployment, error) { - params := []string{"get", fmt.Sprintf("machinedeployments.%s", clusterv1.GroupVersion.Group), "-o", "json"} + params := []string{"get", capiMachineDeploymentsType, "-o", "json"} applyOpts(¶ms, opts...) stdOut, err := k.Execute(ctx, params...) if err != nil { @@ -2241,7 +2223,7 @@ func (k *Kubectl) AllTinkerbellHardware(ctx context.Context, kubeconfig string) // HasCRD checks if the given CRD exists in the cluster specified by kubeconfig. func (k *Kubectl) HasCRD(ctx context.Context, crd, kubeconfig string) (bool, error) { - _, err := k.Execute(ctx, "get", "crd", crd, "--kubeconfig", kubeconfig) + _, err := k.Execute(ctx, "get", "customresourcedefinition", crd, "--kubeconfig", kubeconfig) if err == nil { return true, nil @@ -2256,7 +2238,7 @@ func (k *Kubectl) HasCRD(ctx context.Context, crd, kubeconfig string) (bool, err // DeleteCRD removes the given CRD from the cluster specified in kubeconfig. func (k *Kubectl) DeleteCRD(ctx context.Context, crd, kubeconfig string) error { - _, err := k.Execute(ctx, "delete", "crd", crd, "--kubeconfig", kubeconfig) + _, err := k.Execute(ctx, "delete", "customresourcedefinition", crd, "--kubeconfig", kubeconfig) if err != nil && !strings.Contains(err.Error(), "NotFound") { return err } diff --git a/pkg/executables/kubectl_test.go b/pkg/executables/kubectl_test.go index 16b22131e3ab..dc36ad74f001 100644 --- a/pkg/executables/kubectl_test.go +++ b/pkg/executables/kubectl_test.go @@ -97,64 +97,6 @@ func newKubectlTest(t *testing.T) *kubectlTest { } } -func TestKubectlGetCAPIMachines(t *testing.T) { - g := NewWithT(t) - k, ctx, cluster, e := newKubectl(t) - - machinesResponseBuffer := bytes.Buffer{} - machinesResponseBuffer.WriteString(test.ReadFile(t, "testdata/kubectl_machines_no_conditions.json")) - - tests := []struct { - name string - buffer bytes.Buffer - machineLength int - execErr error - expectedErr error - }{ - { - name: "GetCAPIMachines_Success", - buffer: machinesResponseBuffer, - machineLength: 2, - execErr: nil, - expectedErr: nil, - }, - { - name: "GetCAPIMachines_Success", - buffer: bytes.Buffer{}, - machineLength: 0, - execErr: nil, - expectedErr: errors.New("parsing get machines response: unexpected end of JSON input"), - }, - { - name: "GetCAPIMachines_Success", - buffer: bytes.Buffer{}, - machineLength: 0, - execErr: errors.New("exec error"), - expectedErr: errors.New("getting machines: exec error"), - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - e.EXPECT().Execute(ctx, - "get", "machines.cluster.x-k8s.io", - "-o", "json", - "--kubeconfig", cluster.KubeconfigFile, - "--selector=cluster.x-k8s.io/cluster-name="+cluster.Name, - "--namespace", constants.EksaSystemNamespace, - ).Return(test.buffer, test.execErr) - - machines, err := k.GetCAPIMachines(ctx, cluster, cluster.Name) - if test.expectedErr != nil { - g.Expect(err).To(MatchError(test.expectedErr)) - } else { - g.Expect(err).To(Not(HaveOccurred())) - } - g.Expect(len(machines)).To(Equal(test.machineLength)) - }) - } -} - func TestKubectlApplyManifestSuccess(t *testing.T) { spec := "specfile" @@ -617,11 +559,11 @@ func TestCloudStackWorkerNodesMachineTemplate(t *testing.T) { machineTemplatesBuffer := bytes.NewBufferString(test.ReadFile(t, "testdata/kubectl_no_cs_machineconfigs.json")) k, ctx, _, e := newKubectl(t) expectedParam1 := []string{ - "get", "MachineDeployment", fmt.Sprintf("%s-md-0", clusterName), "-o", "go-template", + "get", "machinedeployments.cluster.x-k8s.io", fmt.Sprintf("%s-md-0", clusterName), "-o", "go-template", "--template", "{{.spec.template.spec.infrastructureRef.name}}", "--kubeconfig", kubeconfig, "--namespace", namespace, } expectedParam2 := []string{ - "get", "cloudstackmachinetemplates", machineTemplateName, "-o", "go-template", "--template", + "get", "cloudstackmachinetemplates.infrastructure.cluster.x-k8s.io", machineTemplateName, "-o", "go-template", "--template", "{{.spec.template.spec}}", "-o", "yaml", "--kubeconfig", kubeconfig, "--namespace", namespace, } e.EXPECT().Execute(ctx, gomock.Eq(expectedParam1)).Return(*machineTemplateNameBuffer, nil) @@ -1425,7 +1367,7 @@ func TestKubectlRolloutRestartDaemonSetSuccess(t *testing.T) { e.EXPECT().Execute( ctx, []string{ - "rollout", "restart", "ds", "cilium", + "rollout", "restart", "daemonset", "cilium", "--kubeconfig", cluster.KubeconfigFile, "--namespace", constants.KubeSystemNamespace, }, ).Return(bytes.Buffer{}, nil) @@ -1441,7 +1383,7 @@ func TestKubectlRolloutRestartDaemonSetError(t *testing.T) { e.EXPECT().Execute( ctx, []string{ - "rollout", "restart", "ds", "cilium", + "rollout", "restart", "daemonset", "cilium", "--kubeconfig", cluster.KubeconfigFile, "--namespace", constants.KubeSystemNamespace, }, ).Return(bytes.Buffer{}, fmt.Errorf("error")) @@ -1981,7 +1923,7 @@ func TestKubectlVersion(t *testing.T) { func TestKubectlValidateClustersCRDSuccess(t *testing.T) { k, ctx, cluster, e := newKubectl(t) - e.EXPECT().Execute(ctx, []string{"get", "crd", "clusters.cluster.x-k8s.io", "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, nil) + e.EXPECT().Execute(ctx, []string{"get", "customresourcedefinition", "clusters.cluster.x-k8s.io", "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, nil) err := k.ValidateClustersCRD(ctx, cluster) if err != nil { t.Fatalf("Kubectl.ValidateClustersCRD() error = %v, want nil", err) @@ -1990,7 +1932,7 @@ func TestKubectlValidateClustersCRDSuccess(t *testing.T) { func TestKubectlValidateClustersCRDNotFound(t *testing.T) { k, ctx, cluster, e := newKubectl(t) - e.EXPECT().Execute(ctx, []string{"get", "crd", "clusters.cluster.x-k8s.io", "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, errors.New("CRD not found")) + e.EXPECT().Execute(ctx, []string{"get", "customresourcedefinition", "clusters.cluster.x-k8s.io", "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, errors.New("CRD not found")) err := k.ValidateClustersCRD(ctx, cluster) if err == nil { t.Fatalf("Kubectl.ValidateClustersCRD() error == nil, want CRD not found") @@ -2774,7 +2716,7 @@ func TestKubectlDeletePackageResources(t *testing.T) { tt := newKubectlTest(t) tt.e.EXPECT().Execute( tt.ctx, - "delete", "pbc", "clusterName", "--kubeconfig", tt.kubeconfig, "--namespace", "eksa-packages", "--ignore-not-found=true", + "delete", "packagebundlecontroller.packages.eks.amazonaws.com", "clusterName", "--kubeconfig", tt.kubeconfig, "--namespace", "eksa-packages", "--ignore-not-found=true", ).Return(*bytes.NewBufferString("//"), nil) tt.e.EXPECT().Execute( tt.ctx, @@ -2788,7 +2730,7 @@ func TestKubectlDeletePackageResources(t *testing.T) { tt := newKubectlTest(t) tt.e.EXPECT().Execute( tt.ctx, - "delete", "pbc", "clusterName", "--kubeconfig", tt.kubeconfig, "--namespace", "eksa-packages", "--ignore-not-found=true", + "delete", "packagebundlecontroller.packages.eks.amazonaws.com", "clusterName", "--kubeconfig", tt.kubeconfig, "--namespace", "eksa-packages", "--ignore-not-found=true", ).Return(*bytes.NewBufferString("//"), fmt.Errorf("bam")) tt.Expect(tt.k.DeletePackageResources(tt.ctx, tt.cluster, "clusterName")).To(MatchError(ContainSubstring("bam"))) @@ -2798,7 +2740,7 @@ func TestKubectlDeletePackageResources(t *testing.T) { tt := newKubectlTest(t) tt.e.EXPECT().Execute( tt.ctx, - "delete", "pbc", "clusterName", "--kubeconfig", tt.kubeconfig, "--namespace", "eksa-packages", "--ignore-not-found=true", + "delete", "packagebundlecontroller.packages.eks.amazonaws.com", "clusterName", "--kubeconfig", tt.kubeconfig, "--namespace", "eksa-packages", "--ignore-not-found=true", ).Return(*bytes.NewBufferString("//"), nil) tt.e.EXPECT().Execute( tt.ctx, @@ -3093,7 +3035,7 @@ func TestGetPackageBundleController(t *testing.T) { t.Errorf("marshaling test service: %s", err) } ret := bytes.NewBuffer(respJSON) - expectedParam := []string{"get", "pbc", "testcluster", "-o", "json", "--kubeconfig", "c.kubeconfig", "--namespace", "eksa-packages", "--ignore-not-found=true"} + expectedParam := []string{"get", "packagebundlecontroller.packages.eks.amazonaws.com", "testcluster", "-o", "json", "--kubeconfig", "c.kubeconfig", "--namespace", "eksa-packages", "--ignore-not-found=true"} tt.e.EXPECT().Execute(gomock.Any(), gomock.Eq(expectedParam)).Return(*ret, nil).AnyTimes() if _, err := tt.k.GetPackageBundleController(tt.ctx, tt.cluster.KubeconfigFile, "testcluster"); err != nil { t.Errorf("Kubectl.GetPackageBundleController() error = %v, want nil", err) @@ -3112,9 +3054,9 @@ func TestGetPackageBundleList(t *testing.T) { t.Errorf("marshaling test service: %s", err) } ret := bytes.NewBuffer(respJSON) - expectedParam := []string{"get", "packagebundles", "-o", "jsonpath='{.items}'", "--kubeconfig", "c.kubeconfig", "-n", "eksa-packages"} + expectedParam := []string{"get", "packagebundles.packages.eks.amazonaws.com", "-o", "jsonpath='{.items}'", "--kubeconfig", "c.kubeconfig", "-n", "eksa-packages"} tt.e.EXPECT().Execute(gomock.Any(), gomock.Eq(expectedParam)).Return(*ret, nil).AnyTimes() - expectedParam = []string{"get", "packagebundle", "-o", "json", "--kubeconfig", "c.kubeconfig", "--namespace", "eksa-packages", "--ignore-not-found=true"} + expectedParam = []string{"get", "packagebundles.packages.eks.amazonaws.com", "-o", "json", "--kubeconfig", "c.kubeconfig", "--namespace", "eksa-packages", "--ignore-not-found=true"} tt.e.EXPECT().Execute(gomock.Any(), gomock.Eq(expectedParam)).Return(*ret, nil).AnyTimes() if _, err := tt.k.GetPackageBundleList(tt.ctx, tt.cluster.KubeconfigFile); err != nil { t.Errorf("Kubectl.GetPackageBundleList() error = %v, want nil", err) @@ -3319,7 +3261,7 @@ func TestKubectlHasCRD(t *testing.T) { const kubeconfig = "kubeconfig" var b bytes.Buffer - params := []string{"get", "crd", crd, "--kubeconfig", kubeconfig} + params := []string{"get", "customresourcedefinition", crd, "--kubeconfig", kubeconfig} e.EXPECT().Execute(ctx, gomock.Eq(params)).Return(b, tt.Error) r, err := k.HasCRD(context.Background(), crd, kubeconfig) @@ -3362,7 +3304,7 @@ func TestKubectlDeleteCRD(t *testing.T) { const kubeconfig = "kubeconfig" var b bytes.Buffer - params := []string{"delete", "crd", crd, "--kubeconfig", kubeconfig} + params := []string{"delete", "customresourcedefinition", crd, "--kubeconfig", kubeconfig} e.EXPECT().Execute(ctx, gomock.Eq(params)).Return(b, tt.Error) err := k.DeleteCRD(context.Background(), crd, kubeconfig) From cc1c652b411c62f001546953492c42b79f7bbecc Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Tue, 21 Mar 2023 16:05:42 -0500 Subject: [PATCH 2/4] Fix unit-test --- pkg/validations/createvalidations/cluster_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/validations/createvalidations/cluster_test.go b/pkg/validations/createvalidations/cluster_test.go index a2b1ebf07ba8..d59a237b0dfe 100644 --- a/pkg/validations/createvalidations/cluster_test.go +++ b/pkg/validations/createvalidations/cluster_test.go @@ -96,8 +96,8 @@ func TestValidateManagementClusterCRDs(t *testing.T) { cluster.Name = testclustername for _, tc := range tests { t.Run(tc.name, func(tt *testing.T) { - e.EXPECT().Execute(ctx, []string{"get", "crd", capiClustersResourceType, "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, tc.errGetClusterCRD).Times(tc.errGetClusterCRDCount) - e.EXPECT().Execute(ctx, []string{"get", "crd", eksaClusterResourceType, "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, tc.errGetEKSAClusterCRD).Times(tc.errGetEKSAClusterCRDCount) + e.EXPECT().Execute(ctx, []string{"get", "customresourcedefinition", capiClustersResourceType, "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, tc.errGetClusterCRD).Times(tc.errGetClusterCRDCount) + e.EXPECT().Execute(ctx, []string{"get", "customresourcedefinition", eksaClusterResourceType, "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, tc.errGetEKSAClusterCRD).Times(tc.errGetEKSAClusterCRDCount) err := createvalidations.ValidateManagementCluster(ctx, k, cluster) if tc.wantErr { assert.Error(tt, err, "expected ValidateManagementCluster to return an error", "test", tc.name) From ddc886c0d605efa8ec856eb3bdc7c260d27faa98 Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Tue, 21 Mar 2023 16:09:33 -0500 Subject: [PATCH 3/4] Fix CAPI Machines --- pkg/executables/kubectl.go | 25 ++++++++++++++ pkg/executables/kubectl_test.go | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/pkg/executables/kubectl.go b/pkg/executables/kubectl.go index 5fdd1ad37d7f..46f8707712ea 100644 --- a/pkg/executables/kubectl.go +++ b/pkg/executables/kubectl.go @@ -92,6 +92,31 @@ type Kubectl struct { Executable } +type capiMachinesResponse struct { + Items []clusterv1.Machine +} + +// GetCAPIMachines returns all the CAPI machines for the provided clusterName. +func (k *Kubectl) GetCAPIMachines(ctx context.Context, cluster *types.Cluster, clusterName string) ([]clusterv1.Machine, error) { + params := []string{ + "get", capiMachinesType, "-o", "json", "--kubeconfig", cluster.KubeconfigFile, + "--selector=cluster.x-k8s.io/cluster-name=" + clusterName, + "--namespace", constants.EksaSystemNamespace, + } + stdOut, err := k.Execute(ctx, params...) + if err != nil { + return nil, fmt.Errorf("getting machines: %v", err) + } + + response := &capiMachinesResponse{} + err = json.Unmarshal(stdOut.Bytes(), response) + if err != nil { + return nil, fmt.Errorf("parsing get machines response: %v", err) + } + + return response.Items, nil +} + func (k *Kubectl) SearchCloudStackMachineConfig(ctx context.Context, name string, kubeconfigFile string, namespace string) ([]*v1alpha1.CloudStackMachineConfig, error) { params := []string{ "get", eksaCloudStackMachineResourceType, "-o", "json", "--kubeconfig", diff --git a/pkg/executables/kubectl_test.go b/pkg/executables/kubectl_test.go index dc36ad74f001..e04ee4cfd5bb 100644 --- a/pkg/executables/kubectl_test.go +++ b/pkg/executables/kubectl_test.go @@ -97,6 +97,64 @@ func newKubectlTest(t *testing.T) *kubectlTest { } } +func TestKubectlGetCAPIMachines(t *testing.T) { + g := NewWithT(t) + k, ctx, cluster, e := newKubectl(t) + + machinesResponseBuffer := bytes.Buffer{} + machinesResponseBuffer.WriteString(test.ReadFile(t, "testdata/kubectl_machines_no_conditions.json")) + + tests := []struct { + name string + buffer bytes.Buffer + machineLength int + execErr error + expectedErr error + }{ + { + name: "GetCAPIMachines_Success", + buffer: machinesResponseBuffer, + machineLength: 2, + execErr: nil, + expectedErr: nil, + }, + { + name: "GetCAPIMachines_Success", + buffer: bytes.Buffer{}, + machineLength: 0, + execErr: nil, + expectedErr: errors.New("parsing get machines response: unexpected end of JSON input"), + }, + { + name: "GetCAPIMachines_Success", + buffer: bytes.Buffer{}, + machineLength: 0, + execErr: errors.New("exec error"), + expectedErr: errors.New("getting machines: exec error"), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + e.EXPECT().Execute(ctx, + "get", "machines.cluster.x-k8s.io", + "-o", "json", + "--kubeconfig", cluster.KubeconfigFile, + "--selector=cluster.x-k8s.io/cluster-name="+cluster.Name, + "--namespace", constants.EksaSystemNamespace, + ).Return(test.buffer, test.execErr) + + machines, err := k.GetCAPIMachines(ctx, cluster, cluster.Name) + if test.expectedErr != nil { + g.Expect(err).To(MatchError(test.expectedErr)) + } else { + g.Expect(err).To(Not(HaveOccurred())) + } + g.Expect(len(machines)).To(Equal(test.machineLength)) + }) + } +} + func TestKubectlApplyManifestSuccess(t *testing.T) { spec := "specfile" From 9923e90aadc115ef88ee5562c97300a38d8fdbe6 Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Wed, 22 Mar 2023 10:21:18 -0500 Subject: [PATCH 4/4] Add uncovered unit-tests - 1 --- pkg/executables/kubectl_test.go | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pkg/executables/kubectl_test.go b/pkg/executables/kubectl_test.go index e04ee4cfd5bb..da1a00d25e60 100644 --- a/pkg/executables/kubectl_test.go +++ b/pkg/executables/kubectl_test.go @@ -632,6 +632,27 @@ func TestCloudStackWorkerNodesMachineTemplate(t *testing.T) { } } +func TestVsphereWorkerNodesMachineTemplate(t *testing.T) { + var kubeconfig, namespace, clusterName, machineTemplateName string + machineTemplateNameBuffer := bytes.NewBufferString(machineTemplateName) + machineTemplatesBuffer := bytes.NewBufferString(test.ReadFile(t, "testdata/kubectl_no_cs_machineconfigs.json")) + k, ctx, _, e := newKubectl(t) + expectedParam1 := []string{ + "get", "machinedeployments.cluster.x-k8s.io", fmt.Sprintf("%s-md-0", clusterName), "-o", "go-template", + "--template", "{{.spec.template.spec.infrastructureRef.name}}", "--kubeconfig", kubeconfig, "--namespace", namespace, + } + expectedParam2 := []string{ + "get", "vspheremachinetemplates.infrastructure.cluster.x-k8s.io", machineTemplateName, "-o", "go-template", "--template", + "{{.spec.template.spec}}", "-o", "yaml", "--kubeconfig", kubeconfig, "--namespace", namespace, + } + e.EXPECT().Execute(ctx, gomock.Eq(expectedParam1)).Return(*machineTemplateNameBuffer, nil) + e.EXPECT().Execute(ctx, gomock.Eq(expectedParam2)).Return(*machineTemplatesBuffer, nil) + _, err := k.VsphereWorkerNodesMachineTemplate(ctx, clusterName, kubeconfig, namespace) + if err != nil { + t.Errorf("Kubectl.GetNamespace() error = %v, want nil", err) + } +} + func TestKubectlSaveLogSuccess(t *testing.T) { filename := "testfile" _, writer := test.NewWriter(t) @@ -1997,6 +2018,24 @@ func TestKubectlValidateClustersCRDNotFound(t *testing.T) { } } +func TestKubectlValidateEKSAClustersCRDSuccess(t *testing.T) { + k, ctx, cluster, e := newKubectl(t) + e.EXPECT().Execute(ctx, []string{"get", "customresourcedefinition", "clusters.anywhere.eks.amazonaws.com", "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, nil) + err := k.ValidateEKSAClustersCRD(ctx, cluster) + if err != nil { + t.Fatalf("Kubectl.ValidateEKSAClustersCRD() error = %v, want nil", err) + } +} + +func TestKubectlValidateEKSAClustersCRDNotFound(t *testing.T) { + k, ctx, cluster, e := newKubectl(t) + e.EXPECT().Execute(ctx, []string{"get", "customresourcedefinition", "clusters.anywhere.eks.amazonaws.com", "--kubeconfig", cluster.KubeconfigFile}).Return(bytes.Buffer{}, errors.New("CRD not found")) + err := k.ValidateEKSAClustersCRD(ctx, cluster) + if err == nil { + t.Fatalf("Kubectl.ValidateEKSAClustersCRD() error == nil, want CRD not found") + } +} + func TestKubectlUpdateEnvironmentVariablesInNamespace(t *testing.T) { k, ctx, cluster, e := newKubectl(t) envMap := map[string]string{