main.go 2.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
	apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/utils/ptr"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/log/zap"
	"sigs.k8s.io/yaml"
)

const (
	fieldManager      = "dynamo-crd-apply"
	versionAnnotation = "dynamo.nvidia.com/operator-version"
)

func main() {
	crdsDir := flag.String("crds-dir", "/opt/dynamo-operator/crds/", "Directory containing CRD YAML files")
	version := flag.String("version", "", "Operator version to stamp on CRDs")
	flag.Parse()

	ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
	log := ctrl.Log.WithName("crd-apply")

	config, err := ctrl.GetConfig()
	if err != nil {
		log.Error(err, "unable to get kubernetes config")
		os.Exit(1)
	}

	client, err := apiextensionsclient.NewForConfig(config)
	if err != nil {
		log.Error(err, "unable to create apiextensions client")
		os.Exit(1)
	}

	entries, err := os.ReadDir(*crdsDir)
	if err != nil {
		log.Error(err, "unable to read CRDs directory", "dir", *crdsDir)
		os.Exit(1)
	}

	ctx := context.Background()
	var applied int

	for _, entry := range entries {
		if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".yaml") {
			continue
		}

		filePath := filepath.Join(*crdsDir, entry.Name())
		data, err := os.ReadFile(filePath)
		if err != nil {
			log.Error(err, "unable to read CRD file", "file", filePath)
			os.Exit(1)
		}

		crd := &apiextensionsv1.CustomResourceDefinition{}
		if err := yaml.Unmarshal(data, crd); err != nil {
			log.Error(err, "unable to unmarshal CRD", "file", filePath)
			os.Exit(1)
		}

		if *version != "" {
			if crd.Annotations == nil {
				crd.Annotations = make(map[string]string)
			}
			crd.Annotations[versionAnnotation] = *version
		}

		patchData, err := yaml.Marshal(crd)
		if err != nil {
			log.Error(err, "unable to marshal CRD for patch", "crd", crd.Name)
			os.Exit(1)
		}

		_, err = client.ApiextensionsV1().CustomResourceDefinitions().Patch(
			ctx,
			crd.Name,
			types.ApplyPatchType,
			patchData,
			metav1.PatchOptions{
				FieldManager: fieldManager,
				Force:        ptr.To(true),
			},
		)
		if err != nil {
			log.Error(err, "unable to apply CRD", "crd", crd.Name)
			os.Exit(1)
		}

		log.Info("Applied CRD", "crd", crd.Name)
		applied++
	}

	if applied == 0 {
		fmt.Fprintf(os.Stderr, "WARNING: no CRD files found in %s\n", *crdsDir)
		os.Exit(1)
	}

	log.Info("CRD apply complete", "applied", applied)
}