This repository has been archived by the owner on May 25, 2022. It is now read-only.
/
crds.go
83 lines (67 loc) · 2.78 KB
/
crds.go
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
// Copyright (C) 2020, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
// Handles creation of CRDs into Managed Clusters
package managed
import (
"context"
"io/ioutil"
"github.com/golang/glog"
v1beta1v8o "github.com/verrazzano/verrazzano-crd-generator/pkg/apis/verrazzano/v1beta1"
"github.com/verrazzano/verrazzano-operator/pkg/util"
"github.com/verrazzano/verrazzano-operator/pkg/util/diff"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
// CreateCrdDefinitions creates/updates custom resource definitions needed for each managed cluster.
func CreateCrdDefinitions(managedClusterConnection *util.ManagedClusterConnection, managedCluster *v1beta1v8o.VerrazzanoManagedCluster, manifest *util.Manifest) error {
glog.V(6).Infof("Creating/updating CRDs for ManagedCluster %s", managedCluster.Name)
managedClusterConnection.Lock.RLock()
managedClusterConnection.Lock.RUnlock()
newCrds, err := newCrds(manifest)
if err != nil {
return err
}
for _, newCrd := range newCrds {
existingCrd, err := managedClusterConnection.KubeExtClientSet.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), newCrd.Name, metav1.GetOptions{})
if err == nil {
specDiffs := diff.CompareIgnoreTargetEmpties(existingCrd, newCrd)
if specDiffs != "" {
glog.V(6).Infof("CRD %s : Spec differences %s", newCrd.Name, specDiffs)
if len(newCrd.ResourceVersion) == 0 {
newCrd.ResourceVersion = existingCrd.ResourceVersion
}
glog.V(4).Infof("Updating CRD %s", newCrd.Name)
_, err = managedClusterConnection.KubeExtClientSet.ApiextensionsV1().CustomResourceDefinitions().Update(context.TODO(), newCrd, metav1.UpdateOptions{})
}
} else {
glog.V(4).Infof("Creating CRD %s", newCrd.Name)
_, err = managedClusterConnection.KubeExtClientSet.ApiextensionsV1().CustomResourceDefinitions().Create(context.TODO(), newCrd, metav1.CreateOptions{})
}
if err != nil {
return err
}
}
return nil
}
// Constructs the necessary CRD Definitions for managed clusters
func newCrds(manifest *util.Manifest) ([]*v1.CustomResourceDefinition, error) {
yamlFiles := []string{manifest.WlsMicroOperatorCrd, manifest.HelidonAppOperatorCrd, manifest.CohClusterOperatorCrd}
var crds []*v1.CustomResourceDefinition
// Loop through the CRD's that need to be generated
for fileIndex := range yamlFiles {
// Read the yaml file that defines the CRD
yamlFile, err := ioutil.ReadFile(yamlFiles[fileIndex])
if err != nil {
return nil, err
}
// Create a CRD struct
var crd v1.CustomResourceDefinition
err = yaml.Unmarshal(yamlFile, &crd)
if err != nil {
return nil, err
}
crds = append(crds, &crd)
}
return crds, nil
}