From cb8c6fa14a3c0b39d26f402595967df4c5bffb29 Mon Sep 17 00:00:00 2001 From: Aravind Ramalingam <60027164+pokearu@users.noreply.github.com> Date: Tue, 11 Jun 2024 19:56:14 -0700 Subject: [PATCH] Refactoring validation and adding airgap support for list packages (#8286) --- cmd/eksctl-anywhere/cmd/generatepackage.go | 4 ---- cmd/eksctl-anywhere/cmd/listpackages.go | 28 ++++++++++++++++++++-- pkg/curatedpackages/validations.go | 12 ++++------ pkg/curatedpackages/validations_test.go | 7 ------ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/cmd/eksctl-anywhere/cmd/generatepackage.go b/cmd/eksctl-anywhere/cmd/generatepackage.go index 37a5501e3fc7..faafa05a85f0 100644 --- a/cmd/eksctl-anywhere/cmd/generatepackage.go +++ b/cmd/eksctl-anywhere/cmd/generatepackage.go @@ -58,10 +58,6 @@ var generatePackageCommand = &cobra.Command{ func runGeneratePackages(cmd *cobra.Command, args []string) error { clusterName := gpOptions.clusterName - if len(gpOptions.kubeVersion) > 0 { - // allow both - clusterName = "" - } if err := curatedpackages.ValidateKubeVersion(gpOptions.kubeVersion, clusterName); err != nil { return err } diff --git a/cmd/eksctl-anywhere/cmd/listpackages.go b/cmd/eksctl-anywhere/cmd/listpackages.go index 2578da994778..5ea97d611350 100644 --- a/cmd/eksctl-anywhere/cmd/listpackages.go +++ b/cmd/eksctl-anywhere/cmd/listpackages.go @@ -6,7 +6,11 @@ import ( "os" "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/types" + anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1" + "github.com/aws/eks-anywhere/pkg/clients/kubernetes" + "github.com/aws/eks-anywhere/pkg/constants" "github.com/aws/eks-anywhere/pkg/curatedpackages" "github.com/aws/eks-anywhere/pkg/kubeconfig" ) @@ -33,7 +37,7 @@ func init() { listPackagesCommand.Flags().StringVar(&lpo.kubeConfig, "kubeconfig", "", "Path to a kubeconfig file to use when source is a cluster.") listPackagesCommand.Flags().StringVar(&lpo.clusterName, "cluster", "", - "Name of cluster for package list.") + "Name of cluster for package list. Required for airgapped environments.") listPackagesCommand.Flags().StringVar(&lpo.bundlesOverride, "bundles-override", "", "Override default Bundles manifest (not recommended)") } @@ -60,7 +64,27 @@ func listPackages(ctx context.Context) error { if err != nil { return err } - deps, err := NewDependenciesForPackages(ctx, WithRegistryName(lpo.registry), WithKubeVersion(lpo.kubeVersion), WithMountPaths(kubeConfig), WithBundlesOverride(lpo.bundlesOverride)) + + depOpts := []PackageOpt{ + WithRegistryName(lpo.registry), + WithKubeVersion(lpo.kubeVersion), + WithMountPaths(kubeConfig), + WithBundlesOverride(lpo.bundlesOverride), + } + cluster := &anywherev1.Cluster{} + if len(lpo.clusterName) > 0 { + k8sClient, err := kubernetes.NewRuntimeClientFromFileName(kubeConfig) + if err != nil { + return fmt.Errorf("unable to initalize k8s client: %v", err) + } + + if err := k8sClient.Get(ctx, types.NamespacedName{Name: lpo.clusterName, Namespace: constants.DefaultNamespace}, cluster); err != nil { + return fmt.Errorf("unable to get cluster %s: %v", lpo.clusterName, err) + } + depOpts = append(depOpts, WithCluster(cluster)) + } + + deps, err := NewDependenciesForPackages(ctx, depOpts...) if err != nil { return fmt.Errorf("unable to initialize executables: %v", err) } diff --git a/pkg/curatedpackages/validations.go b/pkg/curatedpackages/validations.go index 5fa9c98b7f3a..920a8c3312f8 100644 --- a/pkg/curatedpackages/validations.go +++ b/pkg/curatedpackages/validations.go @@ -6,13 +6,6 @@ import ( ) func ValidateKubeVersion(kubeVersion string, clusterName string) error { - if len(clusterName) > 0 { - if len(kubeVersion) > 0 { - return fmt.Errorf("please specify either kube-version or cluster name not both") - } - return nil - } - if len(kubeVersion) > 0 { versionSplit := strings.Split(kubeVersion, ".") if len(versionSplit) != 2 { @@ -20,5 +13,10 @@ func ValidateKubeVersion(kubeVersion string, clusterName string) error { } return nil } + + if len(clusterName) > 0 { + // no-op since either cluster name or kube-version is needed. + return nil + } return fmt.Errorf("please specify kube-version or cluster name") } diff --git a/pkg/curatedpackages/validations_test.go b/pkg/curatedpackages/validations_test.go index 32800e18e636..dc95b026a3b0 100644 --- a/pkg/curatedpackages/validations_test.go +++ b/pkg/curatedpackages/validations_test.go @@ -13,13 +13,6 @@ func TestValidateNoKubeVersionWhenClusterSucceeds(t *testing.T) { } } -func TestValidateKubeVersionWhenClusterFails(t *testing.T) { - err := curatedpackages.ValidateKubeVersion("1.21", "morby") - if err == nil { - t.Errorf("not both kube-version and cluster") - } -} - func TestValidateKubeVersionWhenNoClusterFails(t *testing.T) { err := curatedpackages.ValidateKubeVersion("", "") if err == nil {