Skip to content

Commit

Permalink
Refactoring validation and adding airgap support for list packages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pokearu authored Jun 12, 2024
1 parent 9e72781 commit cb8c6fa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
4 changes: 0 additions & 4 deletions cmd/eksctl-anywhere/cmd/generatepackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 26 additions & 2 deletions cmd/eksctl-anywhere/cmd/listpackages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)")
}
Expand All @@ -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)
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/curatedpackages/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ 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 {
return fmt.Errorf("please specify kube-version as <major>.<minor>")
}
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")
}
7 changes: 0 additions & 7 deletions pkg/curatedpackages/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cb8c6fa

Please sign in to comment.