From 7392306ea9a1354ca9ac42d5d12c3554f919a063 Mon Sep 17 00:00:00 2001 From: Starttoaster Date: Sat, 14 Sep 2024 20:00:54 -0700 Subject: [PATCH] Add ChiaNetwork config option to all chia-deploying resources --- api/v1/chiacommon_types.go | 4 ++ api/v1/zz_generated.deepcopy.go | 5 +++ .../crd/bases/k8s.chia.net_chiacrawlers.yaml | 4 ++ .../crd/bases/k8s.chia.net_chiafarmers.yaml | 4 ++ .../bases/k8s.chia.net_chiaharvesters.yaml | 4 ++ .../bases/k8s.chia.net_chiaintroducers.yaml | 4 ++ config/crd/bases/k8s.chia.net_chianodes.yaml | 4 ++ .../crd/bases/k8s.chia.net_chiaseeders.yaml | 4 ++ .../crd/bases/k8s.chia.net_chiatimelords.yaml | 4 ++ .../crd/bases/k8s.chia.net_chiawallets.yaml | 4 ++ internal/controller/chiacrawler/assemblers.go | 27 ++++++++---- internal/controller/chiacrawler/controller.go | 6 ++- internal/controller/chiacrawler/helpers.go | 13 ++++-- internal/controller/chiafarmer/assemblers.go | 27 ++++++++---- internal/controller/chiafarmer/controller.go | 6 ++- internal/controller/chiafarmer/helpers.go | 13 ++++-- .../controller/chiaharvester/assemblers.go | 27 ++++++++---- .../controller/chiaharvester/controller.go | 6 ++- internal/controller/chiaharvester/helpers.go | 13 ++++-- .../controller/chiaintroducer/assemblers.go | 27 ++++++++---- .../controller/chiaintroducer/controller.go | 6 ++- internal/controller/chiaintroducer/helpers.go | 13 ++++-- internal/controller/chianode/assemblers.go | 26 ++++++++---- internal/controller/chianode/controller.go | 10 +++-- internal/controller/chianode/helpers.go | 12 ++++-- internal/controller/chiaseeder/assemblers.go | 27 ++++++++---- internal/controller/chiaseeder/controller.go | 6 ++- internal/controller/chiaseeder/helpers.go | 13 ++++-- .../controller/chiatimelord/assemblers.go | 27 ++++++++---- .../controller/chiatimelord/controller.go | 6 ++- internal/controller/chiatimelord/helpers.go | 13 ++++-- internal/controller/chiawallet/assemblers.go | 26 ++++++++---- internal/controller/chiawallet/controller.go | 6 ++- internal/controller/chiawallet/helpers.go | 12 ++++-- internal/controller/common/kube/helpers.go | 42 ++++++++++++++++++- 35 files changed, 359 insertions(+), 92 deletions(-) diff --git a/api/v1/chiacommon_types.go b/api/v1/chiacommon_types.go index e25f93b..351bfec 100644 --- a/api/v1/chiacommon_types.go +++ b/api/v1/chiacommon_types.go @@ -84,6 +84,10 @@ type CommonSpecChia struct { // +optional Testnet *bool `json:"testnet,omitempty"` + // ChiaNetwork is the name of a ChiaNetwork resource in the same namespace as this resource + // +optional + ChiaNetwork *string `json:"chiaNetwork,omitempty"` + // Network can be set to a network name in the chia configuration file to switch to // +optional Network *string `json:"network,omitempty"` diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index bf025d3..dd89cd3 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -1322,6 +1322,11 @@ func (in *CommonSpecChia) DeepCopyInto(out *CommonSpecChia) { *out = new(bool) **out = **in } + if in.ChiaNetwork != nil { + in, out := &in.ChiaNetwork, &out.ChiaNetwork + *out = new(string) + **out = **in + } if in.Network != nil { in, out := &in.Network, &out.Network *out = new(string) diff --git a/config/crd/bases/k8s.chia.net_chiacrawlers.yaml b/config/crd/bases/k8s.chia.net_chiacrawlers.yaml index 66c0a1c..eea46b3 100644 --- a/config/crd/bases/k8s.chia.net_chiacrawlers.yaml +++ b/config/crd/bases/k8s.chia.net_chiacrawlers.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chiafarmers.yaml b/config/crd/bases/k8s.chia.net_chiafarmers.yaml index 5931dab..2d5fdca 100644 --- a/config/crd/bases/k8s.chia.net_chiafarmers.yaml +++ b/config/crd/bases/k8s.chia.net_chiafarmers.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chiaharvesters.yaml b/config/crd/bases/k8s.chia.net_chiaharvesters.yaml index 7b0a612..3ccf750 100644 --- a/config/crd/bases/k8s.chia.net_chiaharvesters.yaml +++ b/config/crd/bases/k8s.chia.net_chiaharvesters.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chiaintroducers.yaml b/config/crd/bases/k8s.chia.net_chiaintroducers.yaml index 0a2495f..9f3d385 100644 --- a/config/crd/bases/k8s.chia.net_chiaintroducers.yaml +++ b/config/crd/bases/k8s.chia.net_chiaintroducers.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for introducers. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chianodes.yaml b/config/crd/bases/k8s.chia.net_chianodes.yaml index e8c719a..0f6a340 100644 --- a/config/crd/bases/k8s.chia.net_chianodes.yaml +++ b/config/crd/bases/k8s.chia.net_chianodes.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chiaseeders.yaml b/config/crd/bases/k8s.chia.net_chiaseeders.yaml index 8424740..c0b46bd 100644 --- a/config/crd/bases/k8s.chia.net_chiaseeders.yaml +++ b/config/crd/bases/k8s.chia.net_chiaseeders.yaml @@ -1023,6 +1023,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chiatimelords.yaml b/config/crd/bases/k8s.chia.net_chiatimelords.yaml index 3c7886e..a97e533 100644 --- a/config/crd/bases/k8s.chia.net_chiatimelords.yaml +++ b/config/crd/bases/k8s.chia.net_chiatimelords.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/config/crd/bases/k8s.chia.net_chiawallets.yaml b/config/crd/bases/k8s.chia.net_chiawallets.yaml index f73d0f3..87b575d 100644 --- a/config/crd/bases/k8s.chia.net_chiawallets.yaml +++ b/config/crd/bases/k8s.chia.net_chiawallets.yaml @@ -1019,6 +1019,10 @@ spec: description: CASecretName is the name of the secret that contains the CA crt and key. Not required for seeders. type: string + chiaNetwork: + description: ChiaNetwork is the name of a ChiaNetwork resource + in the same namespace as this resource + type: string daemonService: description: |- DaemonService defines settings for the daemon Service installed with any Chia component resource. diff --git a/internal/controller/chiacrawler/assemblers.go b/internal/controller/chiacrawler/assemblers.go index dfe3030..bfc44ef 100644 --- a/internal/controller/chiacrawler/assemblers.go +++ b/internal/controller/chiacrawler/assemblers.go @@ -5,8 +5,11 @@ Copyright 2024 Chia Network Inc. package chiacrawler import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" appsv1 "k8s.io/api/apps/v1" @@ -235,7 +238,7 @@ func assembleVolumeClaim(crawler k8schianetv1.ChiaCrawler) (*corev1.PersistentVo } // assembleDeployment assembles the crawler Deployment resource for a ChiaCrawler CR -func assembleDeployment(crawler k8schianetv1.ChiaCrawler) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, crawler k8schianetv1.ChiaCrawler) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiacrawlerNamePattern, crawler.Name), @@ -253,7 +256,6 @@ func assembleDeployment(crawler k8schianetv1.ChiaCrawler) appsv1.Deployment { Annotations: crawler.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(crawler)}, Affinity: crawler.Spec.Affinity, NodeSelector: crawler.Spec.NodeSelector, Volumes: getChiaVolumes(crawler), @@ -262,6 +264,12 @@ func assembleDeployment(crawler k8schianetv1.ChiaCrawler) appsv1.Deployment { }, } + chiaContainer, err := assembleChiaContainer(ctx, c, crawler) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(crawler.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range crawler.Spec.InitContainers { @@ -274,7 +282,7 @@ func assembleDeployment(crawler k8schianetv1.ChiaCrawler) appsv1.Deployment { // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(crawler)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -304,14 +312,13 @@ func assembleDeployment(crawler k8schianetv1.ChiaCrawler) appsv1.Deployment { // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(crawler k8schianetv1.ChiaCrawler) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, crawler k8schianetv1.ChiaCrawler) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: crawler.Spec.ChiaConfig.Image, ImagePullPolicy: crawler.Spec.ImagePullPolicy, - Env: getChiaEnv(crawler), Ports: []corev1.ContainerPort{ { Name: "daemon", @@ -332,6 +339,12 @@ func assembleChiaContainer(crawler k8schianetv1.ChiaCrawler) corev1.Container { VolumeMounts: getChiaVolumeMounts(crawler), } + env, err := getChiaEnv(ctx, c, crawler) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if crawler.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = crawler.Spec.ChiaConfig.SecurityContext } @@ -352,7 +365,7 @@ func assembleChiaContainer(crawler k8schianetv1.ChiaCrawler) corev1.Container { input.ResourceRequirements = crawler.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(crawler k8schianetv1.ChiaCrawler) corev1.Container { diff --git a/internal/controller/chiacrawler/controller.go b/internal/controller/chiacrawler/controller.go index 243cf64..1f4a5f7 100644 --- a/internal/controller/chiacrawler/controller.go +++ b/internal/controller/chiacrawler/controller.go @@ -151,7 +151,11 @@ func (r *ChiaCrawlerReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // Assemble Deployment - deploy := assembleDeployment(crawler) + deploy, err := assembleDeployment(ctx, r.Client, crawler) + if err != nil { + r.Recorder.Event(&crawler, corev1.EventTypeWarning, "Failed", "Failed to assemble crawler Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaCrawlerReconciler ChiaCrawler=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&crawler, &deploy, r.Scheme); err != nil { r.Recorder.Event(&crawler, corev1.EventTypeWarning, "Failed", "Failed to assemble crawler Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaCrawlerReconciler ChiaCrawler=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiacrawler/helpers.go b/internal/controller/chiacrawler/helpers.go index c484b77..161d17c 100644 --- a/internal/controller/chiacrawler/helpers.go +++ b/internal/controller/chiacrawler/helpers.go @@ -5,8 +5,11 @@ Copyright 2024 Chia Network Inc. package chiacrawler import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" corev1 "k8s.io/api/core/v1" @@ -73,7 +76,7 @@ func getChiaVolumeMounts(crawler k8schianetv1.ChiaCrawler) []corev1.VolumeMount } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(crawler k8schianetv1.ChiaCrawler) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, crawler k8schianetv1.ChiaCrawler) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // service env var @@ -89,7 +92,11 @@ func getChiaEnv(crawler k8schianetv1.ChiaCrawler) []corev1.EnvVar { }) // Add common env - env = append(env, kube.GetCommonChiaEnv(crawler.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, crawler.ObjectMeta.Namespace, crawler.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/chiafarmer/assemblers.go b/internal/controller/chiafarmer/assemblers.go index 57e2f04..e04a400 100644 --- a/internal/controller/chiafarmer/assemblers.go +++ b/internal/controller/chiafarmer/assemblers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiafarmer import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" appsv1 "k8s.io/api/apps/v1" @@ -235,7 +238,7 @@ func assembleVolumeClaim(farmer k8schianetv1.ChiaFarmer) (*corev1.PersistentVolu } // assembleDeployment assembles the farmer Deployment resource for a ChiaFarmer CR -func assembleDeployment(farmer k8schianetv1.ChiaFarmer) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, farmer k8schianetv1.ChiaFarmer) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiafarmerNamePattern, farmer.Name), @@ -253,7 +256,6 @@ func assembleDeployment(farmer k8schianetv1.ChiaFarmer) appsv1.Deployment { Annotations: farmer.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(farmer)}, Affinity: farmer.Spec.Affinity, NodeSelector: farmer.Spec.NodeSelector, Volumes: getChiaVolumes(farmer), @@ -262,6 +264,12 @@ func assembleDeployment(farmer k8schianetv1.ChiaFarmer) appsv1.Deployment { }, } + chiaContainer, err := assembleChiaContainer(ctx, c, farmer) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(farmer.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range farmer.Spec.InitContainers { @@ -274,7 +282,7 @@ func assembleDeployment(farmer k8schianetv1.ChiaFarmer) appsv1.Deployment { // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(farmer)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -304,14 +312,13 @@ func assembleDeployment(farmer k8schianetv1.ChiaFarmer) appsv1.Deployment { // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(farmer k8schianetv1.ChiaFarmer) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, farmer k8schianetv1.ChiaFarmer) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: farmer.Spec.ChiaConfig.Image, ImagePullPolicy: farmer.Spec.ImagePullPolicy, - Env: getChiaEnv(farmer), Ports: []corev1.ContainerPort{ { Name: "daemon", @@ -332,6 +339,12 @@ func assembleChiaContainer(farmer k8schianetv1.ChiaFarmer) corev1.Container { VolumeMounts: getChiaVolumeMounts(), } + env, err := getChiaEnv(ctx, c, farmer) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if farmer.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = farmer.Spec.ChiaConfig.SecurityContext } @@ -352,7 +365,7 @@ func assembleChiaContainer(farmer k8schianetv1.ChiaFarmer) corev1.Container { input.ResourceRequirements = farmer.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(farmer k8schianetv1.ChiaFarmer) corev1.Container { diff --git a/internal/controller/chiafarmer/controller.go b/internal/controller/chiafarmer/controller.go index e3bfd3a..a70dbd6 100644 --- a/internal/controller/chiafarmer/controller.go +++ b/internal/controller/chiafarmer/controller.go @@ -151,7 +151,11 @@ func (r *ChiaFarmerReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // Assemble Deployment - deploy := assembleDeployment(farmer) + deploy, err := assembleDeployment(ctx, r.Client, farmer) + if err != nil { + r.Recorder.Event(&farmer, corev1.EventTypeWarning, "Failed", "Failed to assemble farmer Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaFarmerReconciler ChiaFarmer=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&farmer, &deploy, r.Scheme); err != nil { r.Recorder.Event(&farmer, corev1.EventTypeWarning, "Failed", "Failed to assemble farmer Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaFarmerReconciler ChiaFarmer=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiafarmer/helpers.go b/internal/controller/chiafarmer/helpers.go index 2d3757e..6e3b102 100644 --- a/internal/controller/chiafarmer/helpers.go +++ b/internal/controller/chiafarmer/helpers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiafarmer import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" k8schianetv1 "github.com/chia-network/chia-operator/api/v1" @@ -78,7 +81,7 @@ func getChiaVolumeMounts() []corev1.VolumeMount { } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(farmer k8schianetv1.ChiaFarmer) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, farmer k8schianetv1.ChiaFarmer) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // service env var @@ -100,7 +103,11 @@ func getChiaEnv(farmer k8schianetv1.ChiaFarmer) []corev1.EnvVar { }) // Add common env - env = append(env, kube.GetCommonChiaEnv(farmer.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, farmer.ObjectMeta.Namespace, farmer.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/chiaharvester/assemblers.go b/internal/controller/chiaharvester/assemblers.go index 6592239..7d746a4 100644 --- a/internal/controller/chiaharvester/assemblers.go +++ b/internal/controller/chiaharvester/assemblers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiaharvester import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" appsv1 "k8s.io/api/apps/v1" @@ -235,7 +238,7 @@ func assembleVolumeClaim(harvester k8schianetv1.ChiaHarvester) (*corev1.Persiste } // assembleDeployment assembles the harvester Deployment resource for a ChiaHarvester CR -func assembleDeployment(harvester k8schianetv1.ChiaHarvester) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, harvester k8schianetv1.ChiaHarvester) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiaharvesterNamePattern, harvester.Name), @@ -253,7 +256,6 @@ func assembleDeployment(harvester k8schianetv1.ChiaHarvester) appsv1.Deployment Annotations: harvester.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(harvester)}, Affinity: harvester.Spec.Affinity, NodeSelector: harvester.Spec.NodeSelector, Volumes: getChiaVolumes(harvester), @@ -262,6 +264,12 @@ func assembleDeployment(harvester k8schianetv1.ChiaHarvester) appsv1.Deployment }, } + chiaContainer, err := assembleChiaContainer(ctx, c, harvester) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(harvester.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range harvester.Spec.InitContainers { @@ -274,7 +282,7 @@ func assembleDeployment(harvester k8schianetv1.ChiaHarvester) appsv1.Deployment // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(harvester)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -304,14 +312,13 @@ func assembleDeployment(harvester k8schianetv1.ChiaHarvester) appsv1.Deployment // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(harvester k8schianetv1.ChiaHarvester) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, harvester k8schianetv1.ChiaHarvester) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: harvester.Spec.ChiaConfig.Image, ImagePullPolicy: harvester.Spec.ImagePullPolicy, - Env: getChiaEnv(harvester), Ports: []corev1.ContainerPort{ { Name: "daemon", @@ -332,6 +339,12 @@ func assembleChiaContainer(harvester k8schianetv1.ChiaHarvester) corev1.Containe VolumeMounts: getChiaVolumeMounts(harvester), } + env, err := getChiaEnv(ctx, c, harvester) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if harvester.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = harvester.Spec.ChiaConfig.SecurityContext } @@ -352,7 +365,7 @@ func assembleChiaContainer(harvester k8schianetv1.ChiaHarvester) corev1.Containe input.ResourceRequirements = harvester.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(harvester k8schianetv1.ChiaHarvester) corev1.Container { diff --git a/internal/controller/chiaharvester/controller.go b/internal/controller/chiaharvester/controller.go index 69bfa57..a0ecf8b 100644 --- a/internal/controller/chiaharvester/controller.go +++ b/internal/controller/chiaharvester/controller.go @@ -151,7 +151,11 @@ func (r *ChiaHarvesterReconciler) Reconcile(ctx context.Context, req ctrl.Reques } // Assemble Deployment - deploy := assembleDeployment(harvester) + deploy, err := assembleDeployment(ctx, r.Client, harvester) + if err != nil { + r.Recorder.Event(&harvester, corev1.EventTypeWarning, "Failed", "Failed to assemble harvester Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaHarvesterReconciler ChiaHarvester=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&harvester, &deploy, r.Scheme); err != nil { r.Recorder.Event(&harvester, corev1.EventTypeWarning, "Failed", "Failed to assemble harvester Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaHarvesterReconciler ChiaHarvester=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiaharvester/helpers.go b/internal/controller/chiaharvester/helpers.go index bb9b6a4..af2cf08 100644 --- a/internal/controller/chiaharvester/helpers.go +++ b/internal/controller/chiaharvester/helpers.go @@ -5,9 +5,12 @@ Copyright 2023 Chia Network Inc. package chiaharvester import ( + "context" "fmt" "strconv" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" k8schianetv1 "github.com/chia-network/chia-operator/api/v1" @@ -139,7 +142,7 @@ func getChiaVolumeMounts(harvester k8schianetv1.ChiaHarvester) []corev1.VolumeMo } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(harvester k8schianetv1.ChiaHarvester) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, harvester k8schianetv1.ChiaHarvester) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // service env var @@ -166,7 +169,11 @@ func getChiaEnv(harvester k8schianetv1.ChiaHarvester) []corev1.EnvVar { }) // Add common env - env = append(env, kube.GetCommonChiaEnv(harvester.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, harvester.ObjectMeta.Namespace, harvester.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/chiaintroducer/assemblers.go b/internal/controller/chiaintroducer/assemblers.go index cb424b7..5ba4648 100644 --- a/internal/controller/chiaintroducer/assemblers.go +++ b/internal/controller/chiaintroducer/assemblers.go @@ -5,8 +5,11 @@ Copyright 2024 Chia Network Inc. package chiaintroducer import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" appsv1 "k8s.io/api/apps/v1" @@ -192,7 +195,7 @@ func assembleVolumeClaim(introducer k8schianetv1.ChiaIntroducer) (*corev1.Persis } // assembleDeployment assembles the introducer Deployment resource for a ChiaIntroducer CR -func assembleDeployment(introducer k8schianetv1.ChiaIntroducer) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, introducer k8schianetv1.ChiaIntroducer) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiaintroducerNamePattern, introducer.Name), @@ -210,7 +213,6 @@ func assembleDeployment(introducer k8schianetv1.ChiaIntroducer) appsv1.Deploymen Annotations: introducer.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(introducer)}, Affinity: introducer.Spec.Affinity, NodeSelector: introducer.Spec.NodeSelector, Volumes: getChiaVolumes(introducer), @@ -219,6 +221,12 @@ func assembleDeployment(introducer k8schianetv1.ChiaIntroducer) appsv1.Deploymen }, } + chiaContainer, err := assembleChiaContainer(ctx, c, introducer) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(introducer.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range introducer.Spec.InitContainers { @@ -231,7 +239,7 @@ func assembleDeployment(introducer k8schianetv1.ChiaIntroducer) appsv1.Deploymen // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(introducer)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -261,14 +269,13 @@ func assembleDeployment(introducer k8schianetv1.ChiaIntroducer) appsv1.Deploymen // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(introducer k8schianetv1.ChiaIntroducer) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, introducer k8schianetv1.ChiaIntroducer) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: introducer.Spec.ChiaConfig.Image, ImagePullPolicy: introducer.Spec.ImagePullPolicy, - Env: getChiaEnv(introducer), Ports: []corev1.ContainerPort{ { Name: "daemon", @@ -284,6 +291,12 @@ func assembleChiaContainer(introducer k8schianetv1.ChiaIntroducer) corev1.Contai VolumeMounts: getChiaVolumeMounts(introducer), } + env, err := getChiaEnv(ctx, c, introducer) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if introducer.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = introducer.Spec.ChiaConfig.SecurityContext } @@ -304,7 +317,7 @@ func assembleChiaContainer(introducer k8schianetv1.ChiaIntroducer) corev1.Contai input.ResourceRequirements = introducer.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(introducer k8schianetv1.ChiaIntroducer) corev1.Container { diff --git a/internal/controller/chiaintroducer/controller.go b/internal/controller/chiaintroducer/controller.go index b8f1a7d..c99cd89 100644 --- a/internal/controller/chiaintroducer/controller.go +++ b/internal/controller/chiaintroducer/controller.go @@ -139,7 +139,11 @@ func (r *ChiaIntroducerReconciler) Reconcile(ctx context.Context, req ctrl.Reque } // Assemble Deployment - deploy := assembleDeployment(introducer) + deploy, err := assembleDeployment(ctx, r.Client, introducer) + if err != nil { + r.Recorder.Event(&introducer, corev1.EventTypeWarning, "Failed", "Failed to assemble introducer Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaIntroducerReconciler ChiaIntroducer=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&introducer, &deploy, r.Scheme); err != nil { r.Recorder.Event(&introducer, corev1.EventTypeWarning, "Failed", "Failed to assemble introducer Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaIntroducerReconciler ChiaIntroducer=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiaintroducer/helpers.go b/internal/controller/chiaintroducer/helpers.go index c9971c7..83eec09 100644 --- a/internal/controller/chiaintroducer/helpers.go +++ b/internal/controller/chiaintroducer/helpers.go @@ -5,8 +5,11 @@ Copyright 2024 Chia Network Inc. package chiaintroducer import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" corev1 "k8s.io/api/core/v1" @@ -73,7 +76,7 @@ func getChiaVolumeMounts(introducer k8schianetv1.ChiaIntroducer) []corev1.Volume } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(introducer k8schianetv1.ChiaIntroducer) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, introducer k8schianetv1.ChiaIntroducer) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // service env var @@ -89,7 +92,11 @@ func getChiaEnv(introducer k8schianetv1.ChiaIntroducer) []corev1.EnvVar { }) // Add common env - env = append(env, kube.GetCommonChiaEnv(introducer.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, introducer.ObjectMeta.Namespace, introducer.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/chianode/assemblers.go b/internal/controller/chianode/assemblers.go index f889bff..fd7253c 100644 --- a/internal/controller/chianode/assemblers.go +++ b/internal/controller/chianode/assemblers.go @@ -8,6 +8,8 @@ import ( "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -259,7 +261,7 @@ func assembleLocalPeerService(node k8schianetv1.ChiaNode) corev1.Service { } // assembleStatefulset assembles the node StatefulSet resource for a ChiaNode CR -func assembleStatefulset(ctx context.Context, node k8schianetv1.ChiaNode) appsv1.StatefulSet { +func assembleStatefulset(ctx context.Context, c client.Client, node k8schianetv1.ChiaNode) (appsv1.StatefulSet, error) { vols, volClaimTemplates := getChiaVolumesAndTemplates(node) stateful := appsv1.StatefulSet{ @@ -282,7 +284,6 @@ func assembleStatefulset(ctx context.Context, node k8schianetv1.ChiaNode) appsv1 }, Spec: corev1.PodSpec{ // TODO add: serviceAccountName config - Containers: []corev1.Container{assembleChiaContainer(ctx, node)}, Affinity: node.Spec.Affinity, NodeSelector: node.Spec.NodeSelector, Volumes: vols, @@ -292,6 +293,12 @@ func assembleStatefulset(ctx context.Context, node k8schianetv1.ChiaNode) appsv1 }, } + chiaContainer, err := assembleChiaContainer(ctx, c, node) + if err != nil { + return appsv1.StatefulSet{}, err + } + stateful.Spec.Template.Spec.Containers = append(stateful.Spec.Template.Spec.Containers, chiaContainer) + if len(node.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range node.Spec.InitContainers { @@ -304,7 +311,7 @@ func assembleStatefulset(ctx context.Context, node k8schianetv1.ChiaNode) appsv1 // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(ctx, node)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } stateful.Spec.Template.Spec.InitContainers = append(stateful.Spec.Template.Spec.InitContainers, cont.Container) @@ -337,14 +344,13 @@ func assembleStatefulset(ctx context.Context, node k8schianetv1.ChiaNode) appsv1 // TODO add pod affinity, tolerations - return stateful + return stateful, nil } -func assembleChiaContainer(ctx context.Context, node k8schianetv1.ChiaNode) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, node k8schianetv1.ChiaNode) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: node.Spec.ChiaConfig.Image, ImagePullPolicy: node.Spec.ImagePullPolicy, - Env: getChiaEnv(ctx, node), Ports: []corev1.ContainerPort{ { Name: "daemon", @@ -365,6 +371,12 @@ func assembleChiaContainer(ctx context.Context, node k8schianetv1.ChiaNode) core VolumeMounts: getChiaVolumeMounts(), } + env, err := getChiaEnv(ctx, c, node) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if node.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = node.Spec.ChiaConfig.SecurityContext } @@ -401,7 +413,7 @@ func assembleChiaContainer(ctx context.Context, node k8schianetv1.ChiaNode) core input.ResourceRequirements = node.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(node k8schianetv1.ChiaNode) corev1.Container { diff --git a/internal/controller/chianode/controller.go b/internal/controller/chianode/controller.go index 86f6f18..783ea6a 100644 --- a/internal/controller/chianode/controller.go +++ b/internal/controller/chianode/controller.go @@ -169,13 +169,17 @@ func (r *ChiaNodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } } - // Assemble Deployment - stateful := assembleStatefulset(ctx, node) + // Assemble StatefulSet + stateful, err := assembleStatefulset(ctx, r.Client, node) + if err != nil { + r.Recorder.Event(&node, corev1.EventTypeWarning, "Failed", "Failed to assemble node Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaNodeReconciler ChiaNode=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&node, &stateful, r.Scheme); err != nil { r.Recorder.Event(&node, corev1.EventTypeWarning, "Failed", "Failed to assemble seeder Statefulset -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaNodeReconciler ChiaNode=%s %v", req.NamespacedName, err) } - // Reconcile Deployment + // Reconcile StatefulSet res, err = kube.ReconcileStatefulset(ctx, r.Client, stateful) if err != nil { r.Recorder.Event(&node, corev1.EventTypeWarning, "Failed", "Failed to create seeder Statefulset -- Check operator logs.") diff --git a/internal/controller/chianode/helpers.go b/internal/controller/chianode/helpers.go index b78eb13..7799ee4 100644 --- a/internal/controller/chianode/helpers.go +++ b/internal/controller/chianode/helpers.go @@ -9,6 +9,8 @@ import ( "encoding/json" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" "sigs.k8s.io/controller-runtime/pkg/log" @@ -120,7 +122,7 @@ func getChiaVolumeMounts() []corev1.VolumeMount { } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(ctx context.Context, node k8schianetv1.ChiaNode) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, node k8schianetv1.ChiaNode) ([]corev1.EnvVar, error) { logr := log.FromContext(ctx) var env []corev1.EnvVar @@ -151,7 +153,11 @@ func getChiaEnv(ctx context.Context, node k8schianetv1.ChiaNode) []corev1.EnvVar } // Add common env - env = append(env, kube.GetCommonChiaEnv(node.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, node.ObjectMeta.Namespace, node.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/chiaseeder/assemblers.go b/internal/controller/chiaseeder/assemblers.go index 0ff1c4c..4fb53fa 100644 --- a/internal/controller/chiaseeder/assemblers.go +++ b/internal/controller/chiaseeder/assemblers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiaseeder import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" appsv1 "k8s.io/api/apps/v1" @@ -294,7 +297,7 @@ func assembleVolumeClaim(seeder k8schianetv1.ChiaSeeder) (*corev1.PersistentVolu } // assembleDeployment assembles the seeder Deployment resource for a ChiaSeeder CR -func assembleDeployment(seeder k8schianetv1.ChiaSeeder) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, seeder k8schianetv1.ChiaSeeder) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiaseederNamePattern, seeder.Name), @@ -312,7 +315,6 @@ func assembleDeployment(seeder k8schianetv1.ChiaSeeder) appsv1.Deployment { Annotations: seeder.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(seeder)}, Affinity: seeder.Spec.Affinity, NodeSelector: seeder.Spec.NodeSelector, Volumes: getChiaVolumes(seeder), @@ -321,6 +323,12 @@ func assembleDeployment(seeder k8schianetv1.ChiaSeeder) appsv1.Deployment { }, } + chiaContainer, err := assembleChiaContainer(ctx, c, seeder) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(seeder.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range seeder.Spec.InitContainers { @@ -333,7 +341,7 @@ func assembleDeployment(seeder k8schianetv1.ChiaSeeder) appsv1.Deployment { // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(seeder)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -366,18 +374,23 @@ func assembleDeployment(seeder k8schianetv1.ChiaSeeder) appsv1.Deployment { // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(seeder k8schianetv1.ChiaSeeder) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, seeder k8schianetv1.ChiaSeeder) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: seeder.Spec.ChiaConfig.Image, ImagePullPolicy: seeder.Spec.ImagePullPolicy, - Env: getChiaEnv(seeder), Ports: getChiaPorts(seeder), VolumeMounts: getChiaVolumeMounts(seeder), } + env, err := getChiaEnv(ctx, c, seeder) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if seeder.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = seeder.Spec.ChiaConfig.SecurityContext } else { @@ -422,7 +435,7 @@ func assembleChiaContainer(seeder k8schianetv1.ChiaSeeder) corev1.Container { input.ResourceRequirements = seeder.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(seeder k8schianetv1.ChiaSeeder) corev1.Container { diff --git a/internal/controller/chiaseeder/controller.go b/internal/controller/chiaseeder/controller.go index 72c9c5b..8dce2cb 100644 --- a/internal/controller/chiaseeder/controller.go +++ b/internal/controller/chiaseeder/controller.go @@ -181,7 +181,11 @@ func (r *ChiaSeederReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // Assemble Deployment - deploy := assembleDeployment(seeder) + deploy, err := assembleDeployment(ctx, r.Client, seeder) + if err != nil { + r.Recorder.Event(&seeder, corev1.EventTypeWarning, "Failed", "Failed to assemble seeder Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaSeederReconciler ChiaSeeder=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&seeder, &deploy, r.Scheme); err != nil { r.Recorder.Event(&seeder, corev1.EventTypeWarning, "Failed", "Failed to assemble seeder Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaSeederReconciler ChiaSeeder=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiaseeder/helpers.go b/internal/controller/chiaseeder/helpers.go index cf607b3..2ee5a4d 100644 --- a/internal/controller/chiaseeder/helpers.go +++ b/internal/controller/chiaseeder/helpers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiaseeder import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" corev1 "k8s.io/api/core/v1" @@ -74,7 +77,7 @@ func getChiaVolumeMounts(seeder k8schianetv1.ChiaSeeder) []corev1.VolumeMount { } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(seeder k8schianetv1.ChiaSeeder) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, seeder k8schianetv1.ChiaSeeder) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // service env var @@ -132,9 +135,13 @@ func getChiaEnv(seeder k8schianetv1.ChiaSeeder) []corev1.EnvVar { } // Add common env - env = append(env, kube.GetCommonChiaEnv(seeder.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, seeder.ObjectMeta.Namespace, seeder.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } // getChiaPorts returns the ports to a chia container diff --git a/internal/controller/chiatimelord/assemblers.go b/internal/controller/chiatimelord/assemblers.go index 1a18776..fe6d586 100644 --- a/internal/controller/chiatimelord/assemblers.go +++ b/internal/controller/chiatimelord/assemblers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiatimelord import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" appsv1 "k8s.io/api/apps/v1" @@ -270,7 +273,7 @@ func assembleVolumeClaim(tl k8schianetv1.ChiaTimelord) (*corev1.PersistentVolume } // assembleDeployment assembles the tl Deployment resource for a ChiaTimelord CR -func assembleDeployment(tl k8schianetv1.ChiaTimelord) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, tl k8schianetv1.ChiaTimelord) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiatimelordNamePattern, tl.Name), @@ -288,7 +291,6 @@ func assembleDeployment(tl k8schianetv1.ChiaTimelord) appsv1.Deployment { Annotations: tl.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(tl)}, Affinity: tl.Spec.Affinity, NodeSelector: tl.Spec.NodeSelector, Volumes: getChiaVolumes(tl), @@ -297,6 +299,12 @@ func assembleDeployment(tl k8schianetv1.ChiaTimelord) appsv1.Deployment { }, } + chiaContainer, err := assembleChiaContainer(ctx, c, tl) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(tl.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range tl.Spec.InitContainers { @@ -309,7 +317,7 @@ func assembleDeployment(tl k8schianetv1.ChiaTimelord) appsv1.Deployment { // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(tl)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -343,14 +351,13 @@ func assembleDeployment(tl k8schianetv1.ChiaTimelord) appsv1.Deployment { // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(tl k8schianetv1.ChiaTimelord) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, tl k8schianetv1.ChiaTimelord) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: tl.Spec.ChiaConfig.Image, ImagePullPolicy: tl.Spec.ImagePullPolicy, - Env: getChiaEnv(tl), Ports: []corev1.ContainerPort{ { Name: "daemon", @@ -371,6 +378,12 @@ func assembleChiaContainer(tl k8schianetv1.ChiaTimelord) corev1.Container { VolumeMounts: getChiaVolumeMounts(), } + env, err := getChiaEnv(ctx, c, tl) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if tl.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = tl.Spec.ChiaConfig.SecurityContext } @@ -407,7 +420,7 @@ func assembleChiaContainer(tl k8schianetv1.ChiaTimelord) corev1.Container { input.ResourceRequirements = tl.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(tl k8schianetv1.ChiaTimelord) corev1.Container { diff --git a/internal/controller/chiatimelord/controller.go b/internal/controller/chiatimelord/controller.go index 4e64d4a..424e5b9 100644 --- a/internal/controller/chiatimelord/controller.go +++ b/internal/controller/chiatimelord/controller.go @@ -166,7 +166,11 @@ func (r *ChiaTimelordReconciler) Reconcile(ctx context.Context, req ctrl.Request } // Assemble Deployment - deploy := assembleDeployment(timelord) + deploy, err := assembleDeployment(ctx, r.Client, timelord) + if err != nil { + r.Recorder.Event(&timelord, corev1.EventTypeWarning, "Failed", "Failed to assemble timelord Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaTimelordReconciler ChiaTimelord=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&timelord, &deploy, r.Scheme); err != nil { r.Recorder.Event(&timelord, corev1.EventTypeWarning, "Failed", "Failed to assemble timelord Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaTimelordReconciler ChiaTimelord=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiatimelord/helpers.go b/internal/controller/chiatimelord/helpers.go index e726b66..70e95fa 100644 --- a/internal/controller/chiatimelord/helpers.go +++ b/internal/controller/chiatimelord/helpers.go @@ -5,8 +5,11 @@ Copyright 2023 Chia Network Inc. package chiatimelord import ( + "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" k8schianetv1 "github.com/chia-network/chia-operator/api/v1" @@ -64,7 +67,7 @@ func getChiaVolumeMounts() []corev1.VolumeMount { } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(timelord k8schianetv1.ChiaTimelord) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, timelord k8schianetv1.ChiaTimelord) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // service env var @@ -80,7 +83,11 @@ func getChiaEnv(timelord k8schianetv1.ChiaTimelord) []corev1.EnvVar { }) // Add common env - env = append(env, kube.GetCommonChiaEnv(timelord.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, timelord.ObjectMeta.Namespace, timelord.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/chiawallet/assemblers.go b/internal/controller/chiawallet/assemblers.go index 12de0e6..5a9ac51 100644 --- a/internal/controller/chiawallet/assemblers.go +++ b/internal/controller/chiawallet/assemblers.go @@ -8,6 +8,8 @@ import ( "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/util/intstr" @@ -235,7 +237,7 @@ func assembleVolumeClaim(wallet k8schianetv1.ChiaWallet) (*corev1.PersistentVolu } // assembleDeployment assembles the wallet Deployment resource for a ChiaWallet CR -func assembleDeployment(ctx context.Context, wallet k8schianetv1.ChiaWallet) appsv1.Deployment { +func assembleDeployment(ctx context.Context, c client.Client, wallet k8schianetv1.ChiaWallet) (appsv1.Deployment, error) { var deploy = appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf(chiawalletNamePattern, wallet.Name), @@ -253,7 +255,6 @@ func assembleDeployment(ctx context.Context, wallet k8schianetv1.ChiaWallet) app Annotations: wallet.Spec.AdditionalMetadata.Annotations, }, Spec: corev1.PodSpec{ - Containers: []corev1.Container{assembleChiaContainer(ctx, wallet)}, Affinity: wallet.Spec.Affinity, NodeSelector: wallet.Spec.NodeSelector, Volumes: getChiaVolumes(wallet), @@ -262,6 +263,12 @@ func assembleDeployment(ctx context.Context, wallet k8schianetv1.ChiaWallet) app }, } + chiaContainer, err := assembleChiaContainer(ctx, c, wallet) + if err != nil { + return appsv1.Deployment{}, err + } + deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, chiaContainer) + if len(wallet.Spec.InitContainers) != 0 { // Overwrite any volumeMounts specified in init containers. Not currently supported. for _, cont := range wallet.Spec.InitContainers { @@ -274,7 +281,7 @@ func assembleDeployment(ctx context.Context, wallet k8schianetv1.ChiaWallet) app // Share chia env if enabled if cont.ShareEnv { - cont.Container.Env = append(cont.Container.Env, getChiaEnv(ctx, wallet)...) + cont.Container.Env = append(cont.Container.Env, chiaContainer.Env...) } deploy.Spec.Template.Spec.InitContainers = append(deploy.Spec.Template.Spec.InitContainers, cont.Container) @@ -304,18 +311,23 @@ func assembleDeployment(ctx context.Context, wallet k8schianetv1.ChiaWallet) app // TODO add pod affinity, tolerations - return deploy + return deploy, nil } -func assembleChiaContainer(ctx context.Context, wallet k8schianetv1.ChiaWallet) corev1.Container { +func assembleChiaContainer(ctx context.Context, c client.Client, wallet k8schianetv1.ChiaWallet) (corev1.Container, error) { input := kube.AssembleChiaContainerInputs{ Image: wallet.Spec.ChiaConfig.Image, ImagePullPolicy: wallet.Spec.ImagePullPolicy, - Env: getChiaEnv(ctx, wallet), Ports: getChiaPorts(), VolumeMounts: getChiaVolumeMounts(), } + env, err := getChiaEnv(ctx, c, wallet) + if err != nil { + return corev1.Container{}, err + } + input.Env = env + if wallet.Spec.ChiaConfig.SecurityContext != nil { input.SecurityContext = wallet.Spec.ChiaConfig.SecurityContext } @@ -336,7 +348,7 @@ func assembleChiaContainer(ctx context.Context, wallet k8schianetv1.ChiaWallet) input.ResourceRequirements = wallet.Spec.ChiaConfig.Resources } - return kube.AssembleChiaContainer(input) + return kube.AssembleChiaContainer(input), nil } func assembleChiaExporterContainer(wallet k8schianetv1.ChiaWallet) corev1.Container { diff --git a/internal/controller/chiawallet/controller.go b/internal/controller/chiawallet/controller.go index 8abd1b0..a99022b 100644 --- a/internal/controller/chiawallet/controller.go +++ b/internal/controller/chiawallet/controller.go @@ -151,7 +151,11 @@ func (r *ChiaWalletReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // Assemble Deployment - deploy := assembleDeployment(ctx, wallet) + deploy, err := assembleDeployment(ctx, r.Client, wallet) + if err != nil { + r.Recorder.Event(&wallet, corev1.EventTypeWarning, "Failed", "Failed to assemble wallet Deployment -- Check operator logs.") + return reconcile.Result{}, fmt.Errorf("ChiaWalletReconciler ChiaWallet=%s %v", req.NamespacedName, err) + } if err := controllerutil.SetControllerReference(&wallet, &deploy, r.Scheme); err != nil { r.Recorder.Event(&wallet, corev1.EventTypeWarning, "Failed", "Failed to assemble wallet Deployment -- Check operator logs.") return reconcile.Result{}, fmt.Errorf("ChiaWalletReconciler ChiaWallet=%s %v", req.NamespacedName, err) diff --git a/internal/controller/chiawallet/helpers.go b/internal/controller/chiawallet/helpers.go index 6934503..1b3b480 100644 --- a/internal/controller/chiawallet/helpers.go +++ b/internal/controller/chiawallet/helpers.go @@ -9,6 +9,8 @@ import ( "encoding/json" "fmt" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/chia-network/chia-operator/internal/controller/common/kube" "sigs.k8s.io/controller-runtime/pkg/log" @@ -102,7 +104,7 @@ func getChiaVolumeMounts() []corev1.VolumeMount { } // getChiaEnv retrieves the environment variables from the Chia config struct -func getChiaEnv(ctx context.Context, wallet k8schianetv1.ChiaWallet) []corev1.EnvVar { +func getChiaEnv(ctx context.Context, c client.Client, wallet k8schianetv1.ChiaWallet) ([]corev1.EnvVar, error) { logr := log.FromContext(ctx) var env []corev1.EnvVar @@ -141,7 +143,11 @@ func getChiaEnv(ctx context.Context, wallet k8schianetv1.ChiaWallet) []corev1.En } // Add common env - env = append(env, kube.GetCommonChiaEnv(wallet.Spec.ChiaConfig.CommonSpecChia)...) + commonEnv, err := kube.GetCommonChiaEnv(ctx, c, wallet.ObjectMeta.Namespace, wallet.Spec.ChiaConfig.CommonSpecChia) + if err != nil { + return env, err + } + env = append(env, commonEnv...) - return env + return env, nil } diff --git a/internal/controller/common/kube/helpers.go b/internal/controller/common/kube/helpers.go index 6ef3491..35df529 100644 --- a/internal/controller/common/kube/helpers.go +++ b/internal/controller/common/kube/helpers.go @@ -5,13 +5,19 @@ Copyright 2023 Chia Network Inc. package kube import ( + "context" + "fmt" "strconv" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/types" + k8schianetv1 "github.com/chia-network/chia-operator/api/v1" "github.com/chia-network/chia-operator/internal/controller/common/consts" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" + "sigs.k8s.io/controller-runtime/pkg/client" ) // GetCommonLabels gives some common labels for chia-operator related objects @@ -143,7 +149,7 @@ func GetExistingChiaRootVolume(storage *k8schianetv1.StorageConfig) corev1.Volum } // GetCommonChiaEnv retrieves the environment variables from the CommonSpecChia config struct -func GetCommonChiaEnv(commonSpecChia k8schianetv1.CommonSpecChia) []corev1.EnvVar { +func GetCommonChiaEnv(ctx context.Context, c client.Client, namespace string, commonSpecChia k8schianetv1.CommonSpecChia) ([]corev1.EnvVar, error) { var env []corev1.EnvVar // CHIA_ROOT env var @@ -233,5 +239,37 @@ func GetCommonChiaEnv(commonSpecChia k8schianetv1.CommonSpecChia) []corev1.EnvVa }) } - return env + // Check for ChiaNetwork, retrieve matching ConfigMap if specified + if commonSpecChia.ChiaNetwork != nil && *commonSpecChia.ChiaNetwork != "" { + var chianetworkConfig corev1.ConfigMap + err := c.Get(ctx, types.NamespacedName{ + Name: *commonSpecChia.ChiaNetwork, + Namespace: namespace, + }, &chianetworkConfig) + if err != nil && errors.IsNotFound(err) { + return []corev1.EnvVar{}, fmt.Errorf("ChiaNetwork specified but its ConfigMap was not found: %v", err) + } else if err != nil { + return []corev1.EnvVar{}, fmt.Errorf("error getting existing ChiaNetwork's ConfigMap: %v", err) + } + + // Loop over data keys, see if any match current environment variables. Overwrite the environment variable, or append a new one + for k, v := range chianetworkConfig.Data { + found := false + for i := range env { + if env[i].Name == k { + env[i].Name = v + found = true + break + } + } + if !found { + env = append(env, corev1.EnvVar{ + Name: k, + Value: v, + }) + } + } + } + + return env, nil }