diff --git a/Makefile b/Makefile index 5f1bea780044..5b1e34e25dcf 100644 --- a/Makefile +++ b/Makefile @@ -162,7 +162,9 @@ eks-a-binary: CGO_ENABLED=0 GOOS=$(GO_OS) GOARCH=$(GO_ARCH) $(GO) build $(BUILD_TAGS_ARG) $(LINKER_FLAGS_ARG) $(BUILD_FLAGS) -o $(OUTPUT_FILE) github.com/aws/eks-anywhere/cmd/eksctl-anywhere .PHONY: eks-a-embed-config +eks-a-embed-config: $(EMBED_CONFIG_FOLDER) eks-a-embed-config: ## Build a dev release version of eks-a with embed cluster spec config + cp pkg/cluster/config/releases.yaml $(EMBED_CONFIG_FOLDER)/releases.yaml $(MAKE) eks-a-binary GIT_VERSION=$(DEV_GIT_VERSION) RELEASE_MANIFEST_URL=embed:///config/releases.yaml BUILD_TAGS='$(BUILD_TAGS) files_embed_fs' .PHONY: eks-a-cross-platform-embed-latest-config @@ -177,6 +179,7 @@ eks-a-cross-platform-embed-latest-config: ## Build cross platform dev release ve .PHONY: eks-a-custom-embed-config eks-a-custom-embed-config: + cp pkg/cluster/config/releases.yaml $(EMBED_CONFIG_FOLDER)/releases.yaml $(MAKE) eks-a-binary GIT_VERSION=$(CUSTOM_GIT_VERSION) RELEASE_MANIFEST_URL=embed:///config/releases.yaml LINKER_FLAGS='-s -w -X github.com/aws/eks-anywhere/pkg/eksctl.enabled=true' BUILD_TAGS='$(BUILD_TAGS) files_embed_fs' .PHONY: eks-a-cross-platform-custom-embed-latest-config diff --git a/cmd/eksctl-anywhere/cmd/version.go b/cmd/eksctl-anywhere/cmd/version.go index b69237f52a11..a5a2429d419e 100644 --- a/cmd/eksctl-anywhere/cmd/version.go +++ b/cmd/eksctl-anywhere/cmd/version.go @@ -1,27 +1,65 @@ package cmd import ( + "encoding/json" "fmt" + "github.com/ghodss/yaml" "github.com/spf13/cobra" "github.com/aws/eks-anywhere/pkg/version" ) +type versionOptions struct { + output string +} + +var vo = &versionOptions{} + var versionCmd = &cobra.Command{ Use: "version", Short: "Get the eksctl anywhere version", Long: "This command prints the version of eksctl anywhere", RunE: func(cmd *cobra.Command, args []string) error { - return printVersion() + return vo.printVersion() }, } func init() { rootCmd.AddCommand(versionCmd) + versionCmd.Flags().StringVarP(&vo.output, "output", "o", "", "specifies the output format (valid option: json)") } -func printVersion() error { - fmt.Println(version.Get().GitVersion) +func (vo *versionOptions) printVersion() error { + versionInfo, bundlesErr := version.GetFullVersionInfo() + switch vo.output { + case "": + fmt.Printf("Version: %s\n", versionInfo.GitVersion) + if bundlesErr != nil { + return fmt.Errorf("error getting bundle manifest URL for version %s: %v", versionInfo, bundlesErr) + } + fmt.Printf("Bundle Manifest URL: %s\n", versionInfo.BundleManifestURL) + case "json": + versionInfoJSON, unmarshalErr := json.Marshal(versionInfo) + if unmarshalErr != nil { + return fmt.Errorf("error marshaling version info to JSON: %v", unmarshalErr) + } + fmt.Printf("%s\n", versionInfoJSON) + if bundlesErr != nil { + return fmt.Errorf("error getting bundle manifest URL for version %s: %v", versionInfo, bundlesErr) + } + case "yaml": + versionInfoYAML, unmarshalErr := yaml.Marshal(versionInfo) + if unmarshalErr != nil { + return fmt.Errorf("error marshaling version info to YAML: %v", unmarshalErr) + } + fmt.Printf("%s\n", versionInfoYAML) + if bundlesErr != nil { + return fmt.Errorf("error getting bundle manifest URL for version %s: %v", versionInfo, bundlesErr) + } + default: + return fmt.Errorf("invalid output format: %s", vo.output) + } + return nil } diff --git a/pkg/manifests/releases/read.go b/pkg/manifests/releases/read.go index e28dff79d4e5..f42d5202634b 100644 --- a/pkg/manifests/releases/read.go +++ b/pkg/manifests/releases/read.go @@ -41,6 +41,22 @@ func ReadReleasesFromURL(reader Reader, url string) (*releasev1.Release, error) return release, nil } +// GetBundleManifestURL fetches the bundle manifest URL pertaining to this +// release version of EKS Anywhere. +func GetBundleManifestURL(reader Reader, version string) (string, error) { + eksAReleases, err := ReadReleases(reader) + if err != nil { + return "", fmt.Errorf("failed to read releases: %v", err) + } + + eksAReleaseForVersion, err := ReleaseForVersion(eksAReleases, version) + if err != nil { + return "", fmt.Errorf("failed to get EKS-A release for version %s: %v", version, err) + } + + return eksAReleaseForVersion.BundleManifestUrl, nil +} + func ReadBundlesForRelease(reader Reader, release *releasev1.EksARelease) (*releasev1.Bundles, error) { return bundles.Read(reader, release.BundleManifestUrl) } diff --git a/pkg/version/version.go b/pkg/version/version.go index 98c0156a5ec4..d3fd0f210e5b 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,9 +1,15 @@ package version +import ( + "github.com/aws/eks-anywhere/pkg/files" + "github.com/aws/eks-anywhere/pkg/manifests/releases" +) + var gitVersion string type Info struct { - GitVersion string + GitVersion string `json:"version"` + BundleManifestURL string `json:"bundleManifestURL,omitempty"` } func Get() Info { @@ -11,3 +17,19 @@ func Get() Info { GitVersion: gitVersion, } } + +// GetFullVersionInfo returns the complete version information for the +// EKS Anywhere, including Git version and bundle manifest URL +// associated with this release. +func GetFullVersionInfo() (Info, error) { + reader := files.NewReader(files.WithEKSAUserAgent("cli", gitVersion)) + bundleManifestURL, err := releases.GetBundleManifestURL(reader, gitVersion) + if err != nil { + return Info{GitVersion: gitVersion}, err + } + + return Info{ + GitVersion: gitVersion, + BundleManifestURL: bundleManifestURL, + }, nil +}