Skip to content

Commit

Permalink
Improve kapp-ctrl alignment (#4074)
Browse files Browse the repository at this point in the history
* Improve kapp-ctrl alignment

Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>

* Minor changes after IRL tests

Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>

* Reduce unnecesary extra deps

Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>

* Merge branch 'master' into 3849-kappctrlAlign

Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>
  • Loading branch information
antgamdia authored Jan 28, 2022
1 parent d3d76c3 commit af5b89f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
corev1 "github.com/kubeapps/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1"
"github.com/kubeapps/kubeapps/cmd/kubeapps-apis/plugins/pkg/paginate"
"github.com/kubeapps/kubeapps/cmd/kubeapps-apis/plugins/pkg/statuserror"
kappctrlinstalled "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/cmd/package/installed"
packagingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
datapackagingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
vendirversions "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions/v1alpha1"
Expand Down Expand Up @@ -505,7 +506,7 @@ func (s *Server) CreateInstalledPackage(ctx context.Context, request *corev1.Cre
}

// build a new pkgInstall object
newPkgInstall, err := s.buildPkgInstall(installedPackageName, targetCluster, targetNamespace, pkgMetadata.Name, pkgVersion, reconciliationOptions)
newPkgInstall, err := s.buildPkgInstall(installedPackageName, targetCluster, targetNamespace, pkgMetadata.Name, pkgVersion, reconciliationOptions, secret)
if err != nil {
return nil, statuserror.FromK8sError("create", "PackageInstall", installedPackageName, err)
}
Expand Down Expand Up @@ -641,6 +642,23 @@ func (s *Server) UpdateInstalledPackage(ctx context.Context, request *corev1.Upd
if updatedSecret == nil || err != nil {
return nil, statuserror.FromK8sError("update", "Secret", secret.Name, err)
}

if updatedSecret != nil {
// Similar logic as in https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.31.0/cli/pkg/kctrl/cmd/package/installed/create_or_update.go#L670
if pkgInstall.ObjectMeta.Annotations == nil {
pkgInstall.ObjectMeta.Annotations = make(map[string]string)
}
pkgInstall.ObjectMeta.Annotations[kappctrlinstalled.KctrlPkgAnnotation+"-"+kappctrlinstalled.KindSecret.AsString()] = fmt.Sprintf(kappctrlinstalled.SecretName, secret.Name, secret.ObjectMeta.Namespace)
pkgInstall.Spec.Values = []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
// The secret name should have the format: <name>-<namespace> as per:
// https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.31.0/cli/pkg/kctrl/cmd/package/installed/created_resource_annotations.go#L19
Name: updatedSecret.Name,
Key: "values.yaml",
},
}}
}

} else {
// Delete all the associated secrets
// TODO(agamez): maybe it's too aggressive and we should be deleting only those secrets created by this plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

corev1 "github.com/kubeapps/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1"
"github.com/kubeapps/kubeapps/cmd/kubeapps-apis/gen/plugins/kapp_controller/packages/v1alpha1"
kappctrlinstalled "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/cmd/package/installed"
kappctrlv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
packagingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
datapackagingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
Expand Down Expand Up @@ -302,21 +303,22 @@ func (s *Server) buildSecret(installedPackageName, values, targetNamespace strin
APIVersion: k8scorev1.SchemeGroupVersion.WithResource(k8scorev1.ResourceSecrets.String()).String(),
},
ObjectMeta: metav1.ObjectMeta{
// TODO(agamez): think about name collisions
Name: fmt.Sprintf("%s-values", installedPackageName),
Name: fmt.Sprintf(kappctrlinstalled.SecretName, installedPackageName, targetNamespace),
Namespace: targetNamespace,
Annotations: map[string]string{
kappctrlinstalled.KctrlPkgAnnotation: fmt.Sprintf("%s-%s", installedPackageName, targetNamespace),
},
},
Data: map[string][]byte{
// TODO(agamez): check the actual value for the key.
// Assuming "values.yaml" perhaps is not always true.
// Perhaos this info is in the "package" object?
// Using "values.yaml" as per:
// https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.31.0/cli/pkg/kctrl/cmd/package/installed/create_or_update.go#L32
"values.yaml": []byte(values),
},
Type: "Opaque",
}, nil
}

func (s *Server) buildPkgInstall(installedPackageName, targetCluster, targetNamespace, packageRefName, pkgVersion string, reconciliationOptions *corev1.ReconciliationOptions) (*packagingv1alpha1.PackageInstall, error) {
func (s *Server) buildPkgInstall(installedPackageName, targetCluster, targetNamespace, packageRefName, pkgVersion string, reconciliationOptions *corev1.ReconciliationOptions, secret *k8scorev1.Secret) (*packagingv1alpha1.PackageInstall, error) {
versionConstraints, err := versionConstraintWithUpgradePolicy(pkgVersion, s.defaultUpgradePolicy)
if err != nil {
return nil, err
Expand All @@ -340,14 +342,6 @@ func (s *Server) buildPkgInstall(installedPackageName, targetCluster, targetName
// Namespace: targetNamespace,
// KubeconfigSecretRef: &kappctrlv1alpha1.AppClusterKubeconfigSecretRef{},
// },
Values: []packagingv1alpha1.PackageInstallValues{
{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: fmt.Sprintf("%s-values", installedPackageName),
Key: "values.yaml",
},
},
},
PackageRef: &packagingv1alpha1.PackageRef{
RefName: packageRefName,
VersionSelection: &vendirversions.VersionSelectionSemver{
Expand All @@ -369,6 +363,23 @@ func (s *Server) buildPkgInstall(installedPackageName, targetCluster, targetName
pkgInstall.Spec.ServiceAccountName = reconciliationOptions.ServiceAccountName
pkgInstall.Spec.Paused = reconciliationOptions.Suspend
}

if secret != nil {
// Similar logic as in https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.31.0/cli/pkg/kctrl/cmd/package/installed/create_or_update.go#L670
if pkgInstall.ObjectMeta.Annotations == nil {
pkgInstall.ObjectMeta.Annotations = make(map[string]string)
}
pkgInstall.ObjectMeta.Annotations[kappctrlinstalled.KctrlPkgAnnotation+"-"+kappctrlinstalled.KindSecret.AsString()] = secret.Name
pkgInstall.Spec.Values = []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
// The secret name should have the format: <name>-<namespace> as per:
// https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.31.0/cli/pkg/kctrl/cmd/package/installed/created_resource_annotations.go#L19
Name: secret.Name,
Key: "values.yaml",
},
}}
}

return pkgInstall, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ func TestGetInstalledPackageSummaries(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -1229,7 +1229,7 @@ func TestGetInstalledPackageSummaries(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -1273,7 +1273,7 @@ func TestGetInstalledPackageSummaries(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -1421,7 +1421,7 @@ func TestGetInstalledPackageSummaries(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -1563,7 +1563,7 @@ func TestGetInstalledPackageSummaries(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -1902,7 +1902,7 @@ func TestGetInstalledPackageDetail(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -1959,7 +1959,7 @@ func TestGetInstalledPackageDetail(t *testing.T) {
&k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-values",
Name: "my-installation-default-values",
},
Type: "Opaque",
Data: map[string][]byte{
Expand Down Expand Up @@ -2393,7 +2393,7 @@ func TestCreateInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -2602,7 +2602,7 @@ func TestCreateInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -2747,7 +2747,7 @@ func TestCreateInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -2890,7 +2890,7 @@ func TestCreateInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -3047,7 +3047,7 @@ func TestUpdateInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -3077,7 +3077,7 @@ func TestUpdateInstalledPackage(t *testing.T) {
&k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-values",
Name: "my-installation-default-values",
},
Type: "Opaque",
Data: map[string][]byte{
Expand Down Expand Up @@ -3112,7 +3112,7 @@ func TestUpdateInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -3219,7 +3219,7 @@ func TestDeleteInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -3249,7 +3249,7 @@ func TestDeleteInstalledPackage(t *testing.T) {
&k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-values",
Name: "my-installation-default-values",
},
Type: "Opaque",
Data: map[string][]byte{
Expand Down Expand Up @@ -3289,7 +3289,7 @@ func TestDeleteInstalledPackage(t *testing.T) {
},
Values: []packagingv1alpha1.PackageInstallValues{{
SecretRef: &packagingv1alpha1.PackageInstallValuesSecretRef{
Name: "my-installation-values",
Name: "my-installation-default-values",
},
},
},
Expand Down Expand Up @@ -3319,7 +3319,7 @@ func TestDeleteInstalledPackage(t *testing.T) {
&k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-installation-values",
Name: "my-installation-default-values",
},
Type: "Opaque",
Data: map[string][]byte{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/urfave/negroni/v2 v2.0.2
github.com/vmware-tanzu/carvel-kapp-controller v0.32.0
github.com/vmware-tanzu/carvel-kapp-controller/cli v0.0.0-20220127145805-87cafa901323
github.com/vmware-tanzu/carvel-vendir v0.24.0
golang.org/x/net v0.0.0-20220121210141-e204ce36a2ba
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
Expand Down Expand Up @@ -155,6 +156,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k14s/difflib v0.0.0-20201117154628-0c031775bf57 // indirect
github.com/k14s/semver/v4 v4.0.1-0.20210701191048-266d47ac6115 // indirect
github.com/k14s/starlark-go v0.0.0-20200720175618-3a5c849cc368 // indirect
github.com/k14s/ytt v0.38.0 // indirect
github.com/klauspost/compress v1.14.2 // indirect
Expand Down
Loading

0 comments on commit af5b89f

Please sign in to comment.