|
|
@ -25,8 +25,9 @@ import (
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
v1 "k8s.io/api/apps/v1"
|
|
|
|
v1 "k8s.io/api/apps/v1"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"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/internal/utils"
|
|
|
|
"github.com/fluxcd/flux2/v2/pkg/manifestgen"
|
|
|
|
"github.com/fluxcd/flux2/v2/pkg/manifestgen"
|
|
|
@ -55,6 +56,12 @@ type versionFlags struct {
|
|
|
|
|
|
|
|
|
|
|
|
var versionArgs versionFlags
|
|
|
|
var versionArgs versionFlags
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type versionInfo struct {
|
|
|
|
|
|
|
|
Flux string `yaml:"flux"`
|
|
|
|
|
|
|
|
Distribution string `yaml:"distribution,omitempty"`
|
|
|
|
|
|
|
|
Controller map[string]string `yaml:"controller,inline"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
func init() {
|
|
|
|
versionCmd.Flags().BoolVar(&versionArgs.client, "client", false,
|
|
|
|
versionCmd.Flags().BoolVar(&versionArgs.client, "client", false,
|
|
|
|
"print only client version")
|
|
|
|
"print only client version")
|
|
|
@ -71,8 +78,12 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
|
|
|
|
defer cancel()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
info := map[string]string{}
|
|
|
|
// versionInfo struct and goyaml is used because we care about the order.
|
|
|
|
info["flux"] = rootArgs.defaults.Version
|
|
|
|
// 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 {
|
|
|
|
if !versionArgs.client {
|
|
|
|
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
|
|
|
|
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
|
|
|
@ -80,6 +91,16 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
|
|
|
|
return err
|
|
|
|
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}
|
|
|
|
selector := client.MatchingLabels{manifestgen.PartOfLabelKey: manifestgen.PartOfLabelValue}
|
|
|
|
var list v1.DeploymentList
|
|
|
|
var list v1.DeploymentList
|
|
|
|
if err := kubeClient.List(ctx, &list, client.InNamespace(*kubeconfigArgs.Namespace), selector); err != nil {
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info[name] = tag
|
|
|
|
info.Controller[name] = tag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -105,7 +126,7 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
|
|
if versionArgs.output == "json" {
|
|
|
|
if versionArgs.output == "json" {
|
|
|
|
marshalled, err = json.MarshalIndent(&info, "", " ")
|
|
|
|
marshalled, err = info.toJSON()
|
|
|
|
marshalled = append(marshalled, "\n"...)
|
|
|
|
marshalled = append(marshalled, "\n"...)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
marshalled, err = yaml.Marshal(&info)
|
|
|
|
marshalled, err = yaml.Marshal(&info)
|
|
|
@ -119,6 +140,20 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
|
|
|
|
return nil
|
|
|
|
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) {
|
|
|
|
func splitImageStr(image string) (string, string, error) {
|
|
|
|
ref, err := name.ParseReference(image)
|
|
|
|
ref, err := name.ParseReference(image)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|