Skip to content

Commit

Permalink
Create user values and kubeconfig secret in app namespace (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiunchinho authored Dec 10, 2020
1 parent 6c41318 commit 58ed443
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add support for setting kubeconfig secret in app CRs for remote clusters.

### Changed

- User config values are created on App namespace.

## [0.7.1] - 2020-11-26

### Fixed
Expand Down
28 changes: 14 additions & 14 deletions apptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

const (
deployedStatus = "deployed"
namespace = "giantswarm"
defaultNamespace = "giantswarm"
uniqueAppCRVersion = "0.0.0"
)

Expand Down Expand Up @@ -266,15 +266,15 @@ func (a *AppSetup) createApps(ctx context.Context, apps []App) error {
if app.AppCRNamespace != "" {
appCRNamespace = app.AppCRNamespace
} else {
appCRNamespace = namespace
appCRNamespace = defaultNamespace
}

var kubeConfig v1alpha1.AppSpecKubeConfig

if app.KubeConfig != "" {
kubeConfigName := fmt.Sprintf("%s-kubeconfig", app.Name)

err := a.createKubeConfigSecret(ctx, kubeConfigName, namespace, app.KubeConfig)
err := a.createKubeConfigSecret(ctx, kubeConfigName, appCRNamespace, app.KubeConfig)
if err != nil {
return microerror.Mask(err)
}
Expand All @@ -286,7 +286,7 @@ func (a *AppSetup) createApps(ctx context.Context, apps []App) error {
InCluster: false,
Secret: v1alpha1.AppSpecKubeConfigSecret{
Name: kubeConfigName,
Namespace: namespace,
Namespace: defaultNamespace,
},
}
} else {
Expand All @@ -300,7 +300,7 @@ func (a *AppSetup) createApps(ctx context.Context, apps []App) error {
if app.ValuesYAML != "" {
userValuesConfigMap = fmt.Sprintf("%s-user-values", app.Name)

err := a.createUserValuesConfigMap(ctx, userValuesConfigMap, namespace, app.ValuesYAML)
err := a.createUserValuesConfigMap(ctx, userValuesConfigMap, appCRNamespace, app.ValuesYAML)
if err != nil {
return microerror.Mask(err)
}
Expand All @@ -324,7 +324,7 @@ func (a *AppSetup) createApps(ctx context.Context, apps []App) error {

if userValuesConfigMap != "" {
appCR.Spec.UserConfig.ConfigMap.Name = userValuesConfigMap
appCR.Spec.UserConfig.ConfigMap.Namespace = namespace
appCR.Spec.UserConfig.ConfigMap.Namespace = defaultNamespace
}

err = a.ctrlClient.Create(ctx, appCR)
Expand All @@ -342,7 +342,7 @@ func (a *AppSetup) createApps(ctx context.Context, apps []App) error {
}

func (a *AppSetup) createKubeConfigSecret(ctx context.Context, name, namespace, kubeConfig string) error {
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("creating secret %#q", name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("creating secret '%s/%s'", namespace, name))

data := map[string][]byte{
"kubeConfig": []byte(kubeConfig),
Expand All @@ -362,21 +362,21 @@ func (a *AppSetup) createKubeConfigSecret(ctx context.Context, name, namespace,
return microerror.Mask(err)
}

a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("created secret %#q", name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("created secret '%s/%s'", namespace, name))
} else {
_, err := a.k8sClient.CoreV1().Secrets(namespace).Update(ctx, desired, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}

a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("updated existing secret %#q", name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("updated existing secret '%s/%s'", namespace, name))
}

return nil
}

func (a *AppSetup) createUserValuesConfigMap(ctx context.Context, name, namespace, valuesYAML string) error {
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("creating %#q configmap", name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("creating '%s/%s' configmap", namespace, name))

values := map[string]string{
"values": valuesYAML,
Expand All @@ -391,11 +391,11 @@ func (a *AppSetup) createUserValuesConfigMap(ctx context.Context, name, namespac

_, err := a.k8sClient.CoreV1().ConfigMaps(namespace).Create(ctx, configMap, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("already created configmap %#q", name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("already created configmap '%s/%s'", namespace, name))
} else if err != nil {
return microerror.Mask(err)
} else {
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("created configmap %#q", name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("created configmap '%s/%s'", namespace, name))
}

return nil
Expand All @@ -419,7 +419,7 @@ func (a *AppSetup) waitForDeployedApps(ctx context.Context, apps []App) error {
func (a *AppSetup) waitForDeployedApp(ctx context.Context, testApp App) error {
var err error

a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("ensuring %#q app CR is %#q", testApp.Name, deployedStatus))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("ensuring '%s/%s' app CR is %#q", testApp.AppCRNamespace, testApp.Name, deployedStatus))

var app v1alpha1.App

Expand Down Expand Up @@ -448,7 +448,7 @@ func (a *AppSetup) waitForDeployedApp(ctx context.Context, testApp App) error {
return microerror.Mask(err)
}

a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("ensured %#q app CR is deployed", testApp.Name))
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("ensured '%s/%s' app CR is deployed", testApp.AppCRNamespace, testApp.Name))

return nil
}
Expand Down

0 comments on commit 58ed443

Please sign in to comment.