create registry secret if not exists when updating
package resources
import (
+ "k8s.io/apimachinery/pkg/api/errors"
"sync"
"github.com/kalmhq/kalm/controller/api/v1alpha1"
@@ -174,26 +175,21 @@ func (builder *Builder) UpdateDockerRegistry(registry *DockerRegistry) (*DockerR
dockerRegistry := &v1alpha1.DockerRegistry{}
secret := &coreV1.Secret{}
- var err error
-
- wg := sync.WaitGroup{}
- wg.Add(2)
-
- go func() {
- defer wg.Done()
- err = builder.Get("", registry.Name, dockerRegistry)
- }()
+ if err := builder.Get("", registry.Name, dockerRegistry); err != nil {
+ return nil, err
+ }
- go func() {
- defer wg.Done()
- err = builder.Get("kalm-system", controllers.GetRegistryAuthenticationName(registry.Name), secret)
- }()
+ secretName := controllers.GetRegistryAuthenticationName(registry.Name)
- wg.Wait()
+ secretNotExist := false
- if err != nil {
+ if err := builder.Get("kalm-system", secretName, secret); err != nil {
+ if errors.IsNotFound(err) {
+ secretNotExist = true
+ } else {
return nil, err
}
+ }
secret.Data = map[string][]byte{
"username": []byte(registry.Username),
@@ -201,19 +197,21 @@ func (builder *Builder) UpdateDockerRegistry(registry *DockerRegistry) (*DockerR
}
dockerRegistry.Spec = *registry.DockerRegistrySpec
- wg.Add(2)
-
- go func() {
- defer wg.Done()
- err = builder.Update(dockerRegistry)
- }()
-
- go func() {
- defer wg.Done()
- err = builder.Update(secret)
- }()
+ if err := builder.Update(dockerRegistry); err != nil {
+ return nil, err
+ }
- wg.Wait()
+ if secretNotExist {
+ secret.Namespace = "kalm-system"
+ secret.Name = secretName
+ if err := builder.Create(secret); err != nil {
+ return nil, err
+ }
+ } else {
+ if err := builder.Update(secret); err != nil {
+ return nil, err
+ }
+ }
return buildDockerRegistryFromResource(dockerRegistry, secret), nil
}
