Skip to content

Commit

Permalink
Restart storage pods on config changes
Browse files Browse the repository at this point in the history
Hash storage input sources to ensure the pods are restarted if config
has been changed, for example when using defaultConfigOverwrite.
  • Loading branch information
cschwede committed Mar 26, 2024
1 parent a5c5672 commit 2865ef2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
5 changes: 5 additions & 0 deletions api/bases/swift.openstack.org_swiftstorages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ spec:
- type
type: object
type: array
hash:
additionalProperties:
type: string
description: Map of hashes to track e.g. job status
type: object
networkAttachments:
additionalProperties:
items:
Expand Down
3 changes: 3 additions & 0 deletions api/v1beta1/swiftstorage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type SwiftStorageStatus struct {

// NetworkAttachments status of the deployment pods
NetworkAttachments map[string][]string `json:"networkAttachments,omitempty"`

// Map of hashes to track e.g. job status
Hash map[string]string `json:"hash,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

5 changes: 5 additions & 0 deletions config/crd/bases/swift.openstack.org_swiftstorages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ spec:
- type
type: object
type: array
hash:
additionalProperties:
type: string
description: Map of hashes to track e.g. job status
type: object
networkAttachments:
additionalProperties:
items:
Expand Down
36 changes: 35 additions & 1 deletion controllers/swiftstorage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ import (
"github.com/openstack-k8s-operators/swift-operator/pkg/swift"
"github.com/openstack-k8s-operators/swift-operator/pkg/swiftstorage"

"github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
"github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment"
"github.com/openstack-k8s-operators/lib-common/modules/common/pod"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
)

// SwiftStorageReconciler reconciles a SwiftStorage object
Expand Down Expand Up @@ -241,8 +243,19 @@ func (r *SwiftStorageReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrlResult, nil
}

// create hash over all the different input resources to identify if any those changed
// and a restart/recreate is required.
inputHash, hashChanged, err := r.createHashOfInputHashes(instance, envVars)
if err != nil {
return ctrl.Result{}, err
} else if hashChanged {
// Hash changed and instance status should be updated (which will be done by main defer func),
// so we need to return and reconcile again
return ctrl.Result{}, nil
}

// Statefulset with all backend containers
sset := statefulset.NewStatefulSet(swiftstorage.StatefulSet(instance, serviceLabels, serviceAnnotations), 5*time.Second)
sset := statefulset.NewStatefulSet(swiftstorage.StatefulSet(instance, serviceLabels, serviceAnnotations, inputHash), 5*time.Second)
ctrlResult, err = sset.CreateOrPatch(ctx, helper)
if err != nil {
return ctrlResult, err
Expand Down Expand Up @@ -345,6 +358,27 @@ func (r *SwiftStorageReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// createHashOfInputHashes - creates a hash of hashes which gets added to the resources which requires a restart
// if any of the input resources change, like configs, passwords, ...
//
// returns the hash, whether the hash changed (as a bool) and any error
func (r *SwiftStorageReconciler) createHashOfInputHashes(
instance *swiftv1beta1.SwiftStorage,
envVars map[string]env.Setter,
) (string, bool, error) {
var hashMap map[string]string
changed := false
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
hash, err := util.ObjectHash(mergedMapVars)
if err != nil {
return hash, changed, err
}
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
instance.Status.Hash = hashMap
}
return hash, changed, nil
}

func getPodIPInNetwork(swiftPod corev1.Pod, namespace string, networkAttachment string) (string, error) {
networkName := fmt.Sprintf("%s/%s", namespace, networkAttachment)
netStat, err := networkattachment.GetNetworkStatusFromAnnotation(swiftPod.Annotations)
Expand Down
27 changes: 24 additions & 3 deletions pkg/swiftstorage/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

env "github.com/openstack-k8s-operators/lib-common/modules/common/env"
swiftv1beta1 "github.com/openstack-k8s-operators/swift-operator/api/v1beta1"
"github.com/openstack-k8s-operators/swift-operator/pkg/swift"
)
Expand All @@ -35,7 +36,7 @@ func getPorts(port int32, name string) []corev1.ContainerPort {
}
}

func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Container {
func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage, env []corev1.EnvVar) []corev1.Container {
securityContext := swift.GetSecurityContext()

containers := []corev1.Container{
Expand All @@ -55,6 +56,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
Ports: getPorts(swift.AccountServerPort, "account"),
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-account-server", "/etc/swift/account-server.conf.d", "-v"},
Env: env,
},
{
Name: "account-replicator",
Expand All @@ -63,6 +65,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-account-replicator", "/etc/swift/account-server.conf.d", "-v"},
Env: env,
},
{
Name: "account-auditor",
Expand All @@ -71,6 +74,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-account-auditor", "/etc/swift/account-server.conf.d", "-v"},
Env: env,
},
{
Name: "account-reaper",
Expand All @@ -79,6 +83,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-account-reaper", "/etc/swift/account-server.conf.d", "-v"},
Env: env,
},
{
Name: "container-server",
Expand All @@ -88,6 +93,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
Ports: getPorts(swift.ContainerServerPort, "container"),
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-container-server", "/etc/swift/container-server.conf.d", "-v"},
Env: env,
},
{
Name: "container-replicator",
Expand All @@ -96,6 +102,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-container-replicator", "/etc/swift/container-server.conf.d", "-v"},
Env: env,
},
{
Name: "container-auditor",
Expand All @@ -104,6 +111,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-container-replicator", "/etc/swift/container-server.conf.d", "-v"},
Env: env,
},
{
Name: "container-updater",
Expand All @@ -112,6 +120,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-container-replicator", "/etc/swift/container-server.conf.d", "-v"},
Env: env,
},
{
Name: "object-server",
Expand All @@ -121,6 +130,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
Ports: getPorts(swift.ObjectServerPort, "object"),
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-object-server", "/etc/swift/object-server.conf.d", "-v"},
Env: env,
},
{
Name: "object-replicator",
Expand All @@ -129,6 +139,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-object-replicator", "/etc/swift/object-server.conf.d", "-v"},
Env: env,
},
{
Name: "object-auditor",
Expand All @@ -137,6 +148,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-object-replicator", "/etc/swift/object-server.conf.d", "-v"},
Env: env,
},
{
Name: "object-updater",
Expand All @@ -145,6 +157,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-object-replicator", "/etc/swift/object-server.conf.d", "-v"},
Env: env,
},
{
Name: "object-expirer",
Expand All @@ -153,6 +166,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-object-expirer", "/etc/swift/object-expirer.conf.d", "-v"},
Env: env,
},
{
Name: "rsync",
Expand All @@ -162,6 +176,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
Ports: getPorts(swift.RsyncPort, "rsync"),
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/rsync", "--daemon", "--no-detach", "--config=/etc/swift/rsyncd.conf", "--log-file=/dev/stdout"},
Env: env,
},
{
Name: "swift-recon-cron",
Expand All @@ -170,6 +185,7 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"sh", "-c", "while true; do /usr/bin/swift-recon-cron /etc/swift/object-server.conf.d -v; sleep 300; done"},
Env: env,
},
}

Expand All @@ -181,19 +197,24 @@ func getStorageContainers(swiftstorage *swiftv1beta1.SwiftStorage) []corev1.Cont
SecurityContext: &securityContext,
VolumeMounts: getStorageVolumeMounts(),
Command: []string{"/usr/bin/swift-container-sharder", "/etc/swift/container-server.conf.d", "-v"},
Env: env,
})
}

return containers
}

func StatefulSet(
swiftstorage *swiftv1beta1.SwiftStorage, labels map[string]string, annotations map[string]string) *appsv1.StatefulSet {
swiftstorage *swiftv1beta1.SwiftStorage, labels map[string]string, annotations map[string]string, configHash string) *appsv1.StatefulSet {

trueVal := true
OnRootMismatch := corev1.FSGroupChangeOnRootMismatch
user := int64(swift.RunAsUser)

envVars := map[string]env.Setter{}
envVars["CONFIG_HASH"] = env.SetValue(configHash)
env := env.MergeEnvs([]corev1.EnvVar{}, envVars)

return &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: swiftstorage.Name,
Expand Down Expand Up @@ -227,7 +248,7 @@ func StatefulSet(
},
},
Volumes: getStorageVolumes(swiftstorage),
Containers: getStorageContainers(swiftstorage),
Containers: getStorageContainers(swiftstorage, env),
Affinity: swift.GetPodAffinity(ComponentName),
},
},
Expand Down

0 comments on commit 2865ef2

Please sign in to comment.