Skip to content

Commit

Permalink
Ensure controllerUUID annotation set correctly for migrated models;
Browse files Browse the repository at this point in the history
  • Loading branch information
ycliuhw committed Jun 22, 2021
1 parent fd9ccb8 commit d9aee5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
40 changes: 37 additions & 3 deletions caas/kubernetes/provider/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,47 @@ func newK8sBroker(
Add(utils.AnnotationModelUUIDKey(isLegacy), modelUUID),
isLegacyLabels: isLegacy,
}
if controllerUUID != "" {
// controllerUUID could be empty in add-k8s without -c because there might be no controller yet.
client.annotations.Add(utils.AnnotationControllerUUIDKey(isLegacy), controllerUUID)
if err := client.ensureNamespaceAnnotationForControllerUUID(controllerUUID, isLegacy); err != nil {
return nil, errors.Trace(err)
}
return client, nil
}

func (k *kubernetesClient) ensureNamespaceAnnotationForControllerUUID(controllerUUID string, isLegacy bool) error {
if len(controllerUUID) == 0 {
// controllerUUID could be empty in add-k8s without -c because there might be no controller yet.
return nil
}
ns, err := k.getNamespaceByName(k.namespace)
if errors.IsNotFound(err) {
return nil
}
if err != nil {
return errors.Trace(err)
}
nsAnnotation := k8sannotations.New(ns.GetAnnotations())
if !nsAnnotation.HasAll(k.annotations) {
// This should never happen unless we changed annotations for a new juju version.
// But in this case, we should have already managed to fix it in upgrade steps.
return errors.NewNotValid(nil,
fmt.Sprintf("namespace has annotations %v but %v is expected", ns.GetAnnotations(), k.annotations),
)
}
k.annotations.Add(utils.AnnotationControllerUUIDKey(isLegacy), controllerUUID)
if nsAnnotation[utils.AnnotationControllerUUIDKey(isLegacy)] == controllerUUID {
logger.Criticalf("controllerUUID %q not changed!!!", controllerUUID)
return nil
}
logger.Criticalf("model %q was migrated from controller %q to %q!!!", k.namespace,
nsAnnotation[utils.AnnotationControllerUUIDKey(isLegacy)], controllerUUID,
)
if err := k.ensureNamespaceAnnotations(ns); err != nil {
return errors.Trace(err)
}
_, err = k.client().CoreV1().Namespaces().Update(context.TODO(), ns, v1.UpdateOptions{})
return errors.Trace(err)
}

// GetAnnotations returns current namespace's annotations.
func (k *kubernetesClient) GetAnnotations() k8sannotations.Annotation {
return k.annotations
Expand Down
9 changes: 5 additions & 4 deletions caas/kubernetes/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,17 @@ func (p kubernetesEnvironProvider) Open(args environs.OpenParams) (caas.Broker,
if args.Config.Name() != environsbootstrap.ControllerModelName {
return broker, nil
}

ns, err := controllerCorelation(broker)
// Opening a controller model.
nsName, err := controllerCorelation(broker)
if errors.IsNotFound(err) {
// The controller is currently bootstrapping.
return broker, nil
} else if err != nil {
return broker, err
return nil, err
}

return newK8sBroker(
args.ControllerUUID, k8sRestConfig, args.Config, ns,
args.ControllerUUID, k8sRestConfig, args.Config, nsName,
NewK8sClients, newRestClient, k8swatcher.NewKubernetesNotifyWatcher, k8swatcher.NewKubernetesStringsWatcher,
utils.RandomPrefix, jujuclock.WallClock)
}
Expand Down

0 comments on commit d9aee5d

Please sign in to comment.