github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/crd/crd.go (about) 1 package crd 2 3 import ( 4 "errors" 5 "strings" 6 7 "github.com/caos/orbos/internal/operator/boom/cmd" 8 "github.com/caos/orbos/pkg/kubernetes" 9 "github.com/caos/orbos/pkg/labels" 10 ctrl "sigs.k8s.io/controller-runtime" 11 12 toolsetslatest "github.com/caos/orbos/internal/operator/boom/api/latest" 13 "github.com/caos/orbos/internal/operator/boom/bundle" 14 bundleconfig "github.com/caos/orbos/internal/operator/boom/bundle/config" 15 "github.com/caos/orbos/internal/operator/boom/crd/config" 16 "github.com/caos/orbos/internal/operator/boom/metrics" 17 "github.com/caos/orbos/internal/operator/boom/name" 18 "github.com/caos/orbos/internal/utils/clientgo" 19 "github.com/caos/orbos/mntr" 20 ) 21 22 const ( 23 version name.Version = "latest" 24 ) 25 26 type Crd struct { 27 bundle *bundle.Bundle 28 monitor mntr.Monitor 29 status error 30 } 31 32 func (c *Crd) GetStatus() error { 33 return c.status 34 } 35 36 func (c *Crd) SetBackStatus() { 37 c.status = nil 38 } 39 40 func (c *Crd) CleanUp() { 41 if c.GetStatus() != nil { 42 return 43 } 44 45 c.status = c.bundle.CleanUp() 46 } 47 48 func GetVersion() name.Version { 49 return version 50 } 51 52 func New(conf *config.Config) *Crd { 53 crdMonitor := conf.Monitor.WithFields(map[string]interface{}{ 54 "version": GetVersion(), 55 }) 56 57 return &Crd{ 58 monitor: crdMonitor, 59 status: nil, 60 } 61 } 62 63 func (c *Crd) SetBundle(conf *bundleconfig.Config) { 64 if c.GetStatus() != nil { 65 return 66 } 67 bundle := bundle.New(conf) 68 69 c.status = bundle.AddApplicationsByBundleName(conf.BundleName) 70 if c.status != nil { 71 return 72 } 73 74 c.bundle = bundle 75 } 76 77 func (c *Crd) GetBundle() *bundle.Bundle { 78 return c.bundle 79 } 80 81 func (c *Crd) Reconcile(currentResourceList []*clientgo.Resource, toolsetCRD *toolsetslatest.Toolset, gitops bool) { 82 if c.GetStatus() != nil { 83 return 84 } 85 86 logFields := map[string]interface{}{ 87 "CRD": toolsetCRD.Metadata.Name, 88 "action": "reconciling", 89 } 90 monitor := c.monitor.WithFields(logFields) 91 92 if toolsetCRD == nil { 93 c.status = errors.New("ToolsetCRD is nil") 94 monitor.Error(c.status) 95 return 96 } 97 98 if c.bundle == nil { 99 c.status = errors.New("no bundle for crd") 100 monitor.Error(c.status) 101 return 102 } 103 104 boomSpec := toolsetCRD.Spec.Boom 105 if boomSpec != nil && boomSpec.SelfReconciling && boomSpec.Version != "" { 106 conf, err := ctrl.GetConfig() 107 if err != nil { 108 c.status = err 109 return 110 } 111 112 k8sClient, err := kubernetes.NewK8sClientWithConfig(monitor, conf) 113 if err != nil { 114 c.status = err 115 return 116 } 117 118 apiVersion := toolsetCRD.APIVersion 119 apiVersionSplit := strings.Split(apiVersion, "/") 120 if len(apiVersionSplit) == 2 { 121 apiVersion = apiVersionSplit[1] 122 } 123 124 if err := cmd.Reconcile( 125 monitor, 126 labels.MustForAPI(labels.MustForOperator("ORBOS", "boom.caos.ch", boomSpec.Version), toolsetCRD.Kind, apiVersion), 127 k8sClient, 128 boomSpec, 129 boomSpec.Version, 130 gitops, 131 ); err != nil { 132 c.status = err 133 return 134 } 135 } else { 136 monitor.Info("not reconciling BOOM itself as selfReconciling is not specified to true or version is empty") 137 } 138 139 c.status = c.bundle.Reconcile(currentResourceList, toolsetCRD.Spec) 140 if c.status != nil { 141 metrics.FailureReconcilingBundle(c.bundle.GetPredefinedBundle()) 142 return 143 } 144 metrics.SuccessfulReconcilingBundle(c.bundle.GetPredefinedBundle()) 145 }