1
0
mirror of synced 2026-02-13 13:06:56 +00:00

show cluster instance name and version in flux check and flux version

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
Somtochi Onyekwere
2023-11-22 21:01:13 +01:00
parent 62ac960273
commit fd163ddcf2
5 changed files with 122 additions and 49 deletions

View File

@@ -25,8 +25,9 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
"sigs.k8s.io/yaml/goyaml.v2"
"github.com/fluxcd/flux2/v2/internal/utils"
"github.com/fluxcd/flux2/v2/pkg/manifestgen"
@@ -55,6 +56,12 @@ type versionFlags struct {
var versionArgs versionFlags
type versionInfo struct {
Flux string `yaml:"flux"`
Distribution string `yaml:"distribution,omitempty"`
Controller map[string]string `yaml:"controller,inline"`
}
func init() {
versionCmd.Flags().BoolVar(&versionArgs.client, "client", false,
"print only client version")
@@ -71,8 +78,12 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()
info := map[string]string{}
info["flux"] = rootArgs.defaults.Version
// versionInfo struct and goyaml is used because we care about the order.
// Without this `distribution` is printed before `flux` when the struct is marshalled.
info := &versionInfo{
Controller: map[string]string{},
}
info.Flux = rootArgs.defaults.Version
if !versionArgs.client {
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
@@ -80,6 +91,16 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
return err
}
clusterInfo, err := getFluxClusterInfo(ctx, kubeClient)
// ignoring not found errors because it means that the GitRepository CRD isn't installed but a user might
// have other controllers(e.g notification-controller), and we want to still return information for them.
if err != nil && !errors.IsNotFound(err) {
return err
}
if clusterInfo.distribution() != "" {
info.Distribution = clusterInfo.distribution()
}
selector := client.MatchingLabels{manifestgen.PartOfLabelKey: manifestgen.PartOfLabelValue}
var list v1.DeploymentList
if err := kubeClient.List(ctx, &list, client.InNamespace(*kubeconfigArgs.Namespace), selector); err != nil {
@@ -96,7 +117,7 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
info[name] = tag
info.Controller[name] = tag
}
}
}
@@ -105,7 +126,7 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
var err error
if versionArgs.output == "json" {
marshalled, err = json.MarshalIndent(&info, "", " ")
marshalled, err = info.toJSON()
marshalled = append(marshalled, "\n"...)
} else {
marshalled, err = yaml.Marshal(&info)
@@ -119,6 +140,20 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
return nil
}
func (info versionInfo) toJSON() ([]byte, error) {
mapInfo := map[string]string{
"flux": info.Flux,
}
if info.Distribution != "" {
mapInfo["distribution"] = info.Distribution
}
for k, v := range info.Controller {
mapInfo[k] = v
}
return json.MarshalIndent(&mapInfo, "", " ")
}
func splitImageStr(image string) (string, string, error) {
ref, err := name.ParseReference(image)
if err != nil {