Skip to content

Commit

Permalink
Add ChiaNetwork config option to all chia-deploying resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Starttoaster committed Sep 15, 2024
1 parent a0ce0a4 commit 7392306
Show file tree
Hide file tree
Showing 35 changed files with 359 additions and 92 deletions.
4 changes: 4 additions & 0 deletions api/v1/chiacommon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiacrawlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiafarmers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiaharvesters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiaintroducers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chianodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiaseeders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiatimelords.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/k8s.chia.net_chiawallets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 20 additions & 7 deletions internal/controller/chiacrawler/assemblers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/chiacrawler/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions internal/controller/chiacrawler/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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
}
27 changes: 20 additions & 7 deletions internal/controller/chiafarmer/assemblers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/chiafarmer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions internal/controller/chiafarmer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Loading

0 comments on commit 7392306

Please sign in to comment.