|
| 1 | +/* |
| 2 | +Copyright The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "k8s.io/kops/tests/e2e/scenarios/ai-conformance/validators/conformance" |
| 28 | +) |
| 29 | + |
| 30 | +func main() { |
| 31 | + ctx := context.Background() |
| 32 | + if err := run(ctx); err != nil { |
| 33 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func run(ctx context.Context) error { |
| 39 | + artifactsDir := os.Getenv("ARTIFACTS") |
| 40 | + if artifactsDir == "" { |
| 41 | + artifactsDir = "_artifacts" |
| 42 | + } |
| 43 | + reportPath := filepath.Join(artifactsDir, "ai-conformance.yaml") |
| 44 | + |
| 45 | + kubernetesVersion := "" |
| 46 | + kopsVersion := "" |
| 47 | + |
| 48 | + if kubernetesVersion == "" { |
| 49 | + v, err := getKubernetesVersion(ctx) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("getting kubernetes version: %v", err) |
| 52 | + } |
| 53 | + kubernetesVersion = v |
| 54 | + } |
| 55 | + |
| 56 | + if kopsVersion == "" { |
| 57 | + v, err := getKopsVersion(ctx) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("getting kOps version: %v", err) |
| 60 | + } |
| 61 | + kopsVersion = v |
| 62 | + } |
| 63 | + |
| 64 | + metadata := conformance.Metadata{ |
| 65 | + KubernetesVersion: kubernetesVersion, |
| 66 | + PlatformName: "kOps", |
| 67 | + PlatformVersion: kopsVersion, |
| 68 | + VendorName: "kOps Project", |
| 69 | + WebsiteURL: "https://kops.sigs.k8s.io/", |
| 70 | + RepoURL: "https://github.com/kubernetes/kops", |
| 71 | + DocumentationURL: "https://kops.sigs.k8s.io/", |
| 72 | + ProductLogoURL: "https://github.com/kubernetes/kops/blob/master/docs/img/logo.png", |
| 73 | + Description: "Kubernetes Operations (kOps) - Production Grade k8s Installation, Upgrades and Management", |
| 74 | + ContactEmailAddress: "sig-cluster-lifecycle@kubernetes.io", |
| 75 | + } |
| 76 | + |
| 77 | + if err := conformance.WriteReport(artifactsDir, metadata); err != nil { |
| 78 | + panic(fmt.Sprintf("Failed to write conformance report: %v", err)) |
| 79 | + } |
| 80 | + |
| 81 | + fmt.Printf("Conformance report written to: %s\n", reportPath) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// getKubernetesVersion retrieves the Kubernetes version by running "kubectl version" and parsing the output. |
| 86 | +func getKubernetesVersion(ctx context.Context) (string, error) { |
| 87 | + cmd := exec.CommandContext(ctx, "kubectl", "version") |
| 88 | + output, err := cmd.Output() |
| 89 | + if err != nil { |
| 90 | + return "", fmt.Errorf("failed to execute kubectl version: %v", err) |
| 91 | + } |
| 92 | + lines := strings.Split(string(output), "\n") |
| 93 | + for _, line := range lines { |
| 94 | + suffix, ok := strings.CutPrefix(line, "Server Version:") |
| 95 | + if ok { |
| 96 | + return strings.TrimSpace(suffix), nil |
| 97 | + } |
| 98 | + } |
| 99 | + return "", fmt.Errorf("server version not found in kubectl output: %q", string(output)) |
| 100 | +} |
| 101 | + |
| 102 | +// getKopsVersion retrieves the kOps version by running "kops version --short". |
| 103 | +func getKopsVersion(ctx context.Context) (string, error) { |
| 104 | + cmd := exec.CommandContext(ctx, "kops", "version", "--short") |
| 105 | + output, err := cmd.Output() |
| 106 | + if err != nil { |
| 107 | + return "", fmt.Errorf("failed to execute kops version: %v", err) |
| 108 | + } |
| 109 | + return strings.TrimSpace(string(output)), nil |
| 110 | +} |
0 commit comments