Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/apis/onecloud/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/rand"

"yunion.io/x/log"
"yunion.io/x/pkg/utils"
Expand Down Expand Up @@ -192,7 +191,7 @@ func SetDefaults_OnecloudCluster(obj *OnecloudCluster) {
if existingLabels == nil {
existingLabels = make(map[string]string)
}
existingLabels[constants.InstanceLabelKey] = fmt.Sprintf("onecloud-cluster-%s", rand.String(4))
existingLabels[constants.InstanceLabelKey] = fmt.Sprintf("onecloud-cluster-%s", obj.GetName())
obj.SetLabels(existingLabels)
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/manager/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ func (m *ComponentManager) syncDaemonSet(
}

func (m *ComponentManager) updateDaemonSet(oc *v1alpha1.OnecloudCluster, newDs, oldDs *apps.DaemonSet) error {
// If selector has changed (e.g. instance label changed), the DaemonSet must be
// deleted and recreated because Kubernetes does not allow selector updates.
if !selectorMatchesLabels(oldDs.Spec.Selector, newDs.Spec.Template.Labels) {
log.Infof("DaemonSet %s selector does not match new template labels, deleting for recreation", oldDs.Name)
if err := m.dsControl.DeleteDaemonSet(oc, oldDs); err != nil {
return err
}
return m.dsControl.CreateDaemonSet(oc, newDs)
}
if !daemonSetEqual(newDs, oldDs) {
ds := *oldDs
ds.Spec.Template = newDs.Spec.Template
Expand Down Expand Up @@ -551,6 +560,18 @@ func (m *ComponentManager) syncDeployment(
}

func (m *ComponentManager) updateDeployment(oc *v1alpha1.OnecloudCluster, newDeploy, oldDeploy *apps.Deployment) error {
// If selector has changed (e.g. instance label changed), the Deployment must be
// deleted and recreated because Kubernetes does not allow selector updates.
if !selectorMatchesLabels(oldDeploy.Spec.Selector, newDeploy.Spec.Template.Labels) {
log.Infof("Deployment %s selector does not match new template labels, deleting for recreation", oldDeploy.Name)
if err := m.deployControl.DeleteDeployment(oc, oldDeploy.Name); err != nil {
return err
}
if err := SetDeploymentLastAppliedConfigAnnotation(newDeploy); err != nil {
return err
}
return m.deployControl.CreateDeployment(oc, newDeploy)
}
if !deploymentEqual(*newDeploy, *oldDeploy) {
deploy := *oldDeploy
deploy.Spec.Template = newDeploy.Spec.Template
Expand Down
1 change: 1 addition & 0 deletions pkg/manager/component/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newLLMManager(man *ComponentManager) manager.ServiceManager {
func (m *llmManager) getProductVersions() []v1alpha1.ProductVersion {
return []v1alpha1.ProductVersion{
v1alpha1.ProductVersionFullStack,
v1alpha1.ProductVersionEdge,
v1alpha1.ProductVersionAI,
}
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/manager/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,23 @@ func daemonSetEqual(new, old *apps.DaemonSet) bool {
return false
}

// selectorMatchesLabels checks whether all key-value pairs in the selector's
// MatchLabels are present in the given labels. This is used to detect when a
// DaemonSet or Deployment's immutable selector no longer matches the desired
// template labels (e.g. after the instance label is regenerated).
func selectorMatchesLabels(selector *v1.LabelSelector, labels map[string]string) bool {
if selector == nil {
return true
}
for k, v := range selector.MatchLabels {
labelVal, exists := labels[k]
if !exists || labelVal != v {
return false
}
}
return true
}

func cronJobEqual(new, old *batchv1.CronJob) bool {
oldConfig := batchv1.CronJob{}
if LastAppliedConfig, ok := old.Annotations[LastAppliedConfigAnnotation]; ok {
Expand Down
Loading