github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/clusters/kubernetes/plugins.go (about) 1 package kubernetes 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "strings" 9 "text/template" 10 11 "github.com/caos/orbos/pkg/helper" 12 13 macherrs "k8s.io/apimachinery/pkg/api/errors" 14 15 "github.com/caos/orbos/internal/executables" 16 "github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra" 17 "github.com/caos/orbos/mntr" 18 "github.com/caos/orbos/pkg/git" 19 "github.com/caos/orbos/pkg/kubernetes" 20 ) 21 22 func ensureK8sPlugins( 23 monitor mntr.Monitor, 24 gitClient *git.Client, 25 k8sClient kubernetes.ClientInt, 26 desired DesiredV0, 27 providerK8sSpec infra.Kubernetes, 28 privateInterface string, 29 ) (err error) { 30 31 k8sPluginsMonitor := monitor 32 33 defer func() { 34 if err != nil { 35 err = fmt.Errorf("ensuring kubernetes plugin resources failed: %w", err) 36 } 37 }() 38 39 if helper.IsNil(k8sClient) { 40 k8sClient = tryToConnect(monitor, desired) 41 } 42 43 if helper.IsNil(k8sClient) { 44 monitor.Info("not ensuring kubernetes plugins as kubeapi is not available") 45 return nil 46 } 47 48 var applyResources io.Reader 49 50 if providerK8sSpec.CleanupAndApply != nil { 51 applyResources, err = providerK8sSpec.CleanupAndApply(k8sClient) 52 if err != nil { 53 return err 54 } 55 } 56 57 if applyResources != nil { 58 k8sPluginsMonitor = k8sPluginsMonitor.WithField("cloudcontrollermanager", providerK8sSpec.CloudController.ProviderName) 59 } 60 61 switch desired.Spec.Networking.Network { 62 case "cilium": 63 64 var create bool 65 deployment, err := k8sClient.GetDeployment("kube-system", "cilium-operator") 66 if macherrs.IsNotFound(err) { 67 create = true 68 err = nil 69 } 70 if err != nil { 71 return err 72 } 73 74 expectVersion := "v1.6.3" 75 if !create && strings.Split(deployment.Spec.Template.Spec.Containers[0].Image, ":")[1] == expectVersion { 76 k8sPluginsMonitor.WithField("version", expectVersion).Debug("Calico is already ensured") 77 break 78 } 79 80 k8sPluginsMonitor = k8sPluginsMonitor.WithField("cilium", expectVersion) 81 82 buf := new(bytes.Buffer) 83 defer buf.Reset() 84 85 istioReg := desired.Spec.CustomImageRegistry 86 if istioReg != "" && !strings.HasSuffix(istioReg, "/") { 87 istioReg += "/" 88 } 89 90 ciliumReg := desired.Spec.CustomImageRegistry 91 if ciliumReg == "" { 92 ciliumReg = "docker.io" 93 } 94 95 if err := template.Must(template.New("").Parse(string(executables.PreBuilt("cilium.yaml")))).Execute(buf, struct { 96 IstioProxyImageRegistry string 97 CiliumImageRegistry string 98 }{ 99 IstioProxyImageRegistry: istioReg, 100 CiliumImageRegistry: ciliumReg, 101 }); err != nil { 102 return err 103 } 104 applyResources = concatYAML(applyResources, buf) 105 106 case "calico": 107 108 var create bool 109 deployment, err := k8sClient.GetDeployment("kube-system", "calico-kube-controllers") 110 if macherrs.IsNotFound(err) { 111 create = true 112 err = nil 113 } 114 if err != nil { 115 return err 116 } 117 118 expectVersion := "v3.18.2" 119 if !create && strings.Split(deployment.Spec.Template.Spec.Containers[0].Image, ":")[1] == expectVersion { 120 k8sPluginsMonitor.WithField("version", expectVersion).Debug("Calico is already ensured") 121 break 122 } 123 124 k8sPluginsMonitor = k8sPluginsMonitor.WithField("calico", expectVersion) 125 126 reg := desired.Spec.CustomImageRegistry 127 if reg != "" && !strings.HasSuffix(reg, "/") { 128 reg += "/" 129 } 130 131 buf := new(bytes.Buffer) 132 defer buf.Reset() 133 if err := template.Must(template.New("").Parse(string(executables.PreBuilt("calico.yaml")))).Execute(buf, struct { 134 ImageRegistry string 135 PrivateInterface string 136 }{ 137 ImageRegistry: reg, 138 PrivateInterface: privateInterface, 139 }); err != nil { 140 return err 141 } 142 applyResources = concatYAML(applyResources, buf) 143 case "": 144 default: 145 networkFile := gitClient.Read(desired.Spec.Networking.Network) 146 if len(networkFile) == 0 { 147 return fmt.Errorf("network file %s is empty or not found in git repository", desired.Spec.Networking.Network) 148 } 149 150 applyResources = concatYAML(applyResources, bytes.NewReader(networkFile)) 151 } 152 if applyResources == nil { 153 return nil 154 } 155 data, err := ioutil.ReadAll(applyResources) 156 if err != nil { 157 return err 158 } 159 if err := k8sClient.ApplyPlainYAML(monitor, data); err != nil { 160 return err 161 } 162 k8sPluginsMonitor.Info("Kubernetes plugins successfully reconciled") 163 return nil 164 }