|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package cmd |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/spf13/cobra" |
| 28 | + "k8s.io/apimachinery/pkg/api/errors" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/client-go/kubernetes" |
| 31 | + |
| 32 | + "github.com/NVIDIA/skyhook/plugin/pkg/client" |
| 33 | + "github.com/NVIDIA/skyhook/plugin/pkg/version" |
| 34 | +) |
| 35 | + |
| 36 | +// NewVersionCmd creates the version command. |
| 37 | +func NewVersionCmd(globals *GlobalOptions) *cobra.Command { |
| 38 | + var timeout time.Duration |
| 39 | + var clientOnly bool |
| 40 | + |
| 41 | + // versionCmd represents the version command |
| 42 | + versionCmd := &cobra.Command{ |
| 43 | + Use: "version", |
| 44 | + Short: "Show plugin and Skyhook operator versions", |
| 45 | + Long: `Display version information for the Skyhook plugin and the Skyhook operator running in the cluster. |
| 46 | +
|
| 47 | +The plugin version is always shown. By default, the command also queries the cluster |
| 48 | +to discover the Skyhook operator version. Use --client-only to skip the cluster query.`, |
| 49 | + Example: ` # Show both plugin and operator versions |
| 50 | + skyhook version |
| 51 | + kubectl skyhook version |
| 52 | +
|
| 53 | + # Show only the plugin version (no cluster query) |
| 54 | + skyhook version --client-only |
| 55 | +
|
| 56 | + # Query operator in a specific namespace |
| 57 | + skyhook version -n skyhook-system`, |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + fmt.Fprintf(cmd.OutOrStdout(), "Skyhook plugin:\t%s\n", version.Summary()) |
| 60 | + |
| 61 | + if clientOnly { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + clientFactory := client.NewFactory(globals.ConfigFlags) |
| 66 | + cli, err := clientFactory.Client() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("initializing kubernetes client: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + ctx, cancel := context.WithTimeout(cmd.Context(), timeout) |
| 72 | + defer cancel() |
| 73 | + |
| 74 | + opVersion, err := discoverOperatorVersion(ctx, cli.Kubernetes(), globals.Namespace()) |
| 75 | + if err != nil { |
| 76 | + fmt.Fprintf(cmd.OutOrStdout(), "Skyhook operator:\tunknown (%v)\n", err) |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + fmt.Fprintf(cmd.OutOrStdout(), "Skyhook operator:\t%s\n", opVersion) |
| 81 | + return nil |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + versionCmd.Flags().DurationVar(&timeout, "timeout", 10*time.Second, "Time limit for contacting the Kubernetes API") |
| 86 | + versionCmd.Flags().BoolVar(&clientOnly, "client-only", false, "Only print the plugin version without querying the cluster") |
| 87 | + |
| 88 | + return versionCmd |
| 89 | +} |
| 90 | + |
| 91 | +func discoverOperatorVersion(ctx context.Context, kube kubernetes.Interface, namespace string) (string, error) { |
| 92 | + if kube == nil { |
| 93 | + return "", fmt.Errorf("nil kubernetes client") |
| 94 | + } |
| 95 | + if namespace == "" { |
| 96 | + namespace = defaultNamespace |
| 97 | + } |
| 98 | + |
| 99 | + deploymentName := "skyhook-operator-controller-manager" |
| 100 | + deployment, err := kube.AppsV1().Deployments(namespace).Get(ctx, deploymentName, metav1.GetOptions{}) |
| 101 | + if err != nil { |
| 102 | + if errors.IsNotFound(err) { |
| 103 | + return "", fmt.Errorf("skyhook operator deployment %q not found in namespace %q", deploymentName, namespace) |
| 104 | + } |
| 105 | + return "", fmt.Errorf("querying operator deployment: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Try to get version from Helm label (preferred for Helm deployments) |
| 109 | + if v := deployment.Labels["app.kubernetes.io/version"]; strings.TrimSpace(v) != "" { |
| 110 | + return strings.TrimSpace(v), nil |
| 111 | + } |
| 112 | + |
| 113 | + // Fallback: parse version from container image tag (works for kustomize deployments) |
| 114 | + if len(deployment.Spec.Template.Spec.Containers) > 0 { |
| 115 | + image := deployment.Spec.Template.Spec.Containers[0].Image |
| 116 | + if tag := extractImageTag(image); tag != "" && tag != "latest" { |
| 117 | + return tag, nil |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return "", fmt.Errorf("unable to determine operator version from deployment labels or image tag") |
| 122 | +} |
| 123 | + |
| 124 | +// extractImageTag extracts the tag from a container image reference. |
| 125 | +// Examples: |
| 126 | +// - "ghcr.io/nvidia/skyhook/operator:v1.2.3" -> "v1.2.3" |
| 127 | +// - "ghcr.io/nvidia/skyhook/operator:v1.2.3@sha256:..." -> "v1.2.3" |
| 128 | +// - "ghcr.io/nvidia/skyhook/operator" -> "" |
| 129 | +func extractImageTag(image string) string { |
| 130 | + // Remove digest if present (e.g., @sha256:...) |
| 131 | + if idx := strings.Index(image, "@"); idx > 0 { |
| 132 | + image = image[:idx] |
| 133 | + } |
| 134 | + |
| 135 | + // Split on ":" to get tag |
| 136 | + parts := strings.Split(image, ":") |
| 137 | + if len(parts) < 2 { |
| 138 | + return "" |
| 139 | + } |
| 140 | + |
| 141 | + return strings.TrimSpace(parts[len(parts)-1]) |
| 142 | +} |
0 commit comments