|
|
@ -22,36 +22,22 @@ import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/selection"
|
|
|
|
|
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
|
|
|
|
|
|
|
|
|
|
|
"helm.sh/helm/v3/pkg/kube"
|
|
|
|
"helm.sh/helm/v3/pkg/kube"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var accessor = meta.NewAccessor()
|
|
|
|
managedByReq, _ = labels.NewRequirement("app.kubernetes.io/managed-by", selection.Equals, []string{"Helm"})
|
|
|
|
|
|
|
|
accessor = meta.NewAccessor()
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newReleaseSelector(release string) (labels.Selector, error) {
|
|
|
|
|
|
|
|
releaseReq, err := labels.NewRequirement("app.kubernetes.io/instance", selection.Equals, []string{release})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return labels.Parse(fmt.Sprintf("%s,%s", releaseReq, managedByReq))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func existingResourceConflict(resources kube.ResourceList, release string) (kube.ResourceList, error) {
|
|
|
|
const (
|
|
|
|
sel, err := newReleaseSelector(release)
|
|
|
|
helmReleaseNameAnnotation = "meta.helm.sh/release-name"
|
|
|
|
if err != nil {
|
|
|
|
helmReleaseNamespaceAnnotation = "meta.helm.sh/release-namespace"
|
|
|
|
return nil, err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func existingResourceConflict(resources kube.ResourceList, releaseName, releaseNamespace string) (kube.ResourceList, error) {
|
|
|
|
requireUpdate := kube.ResourceList{}
|
|
|
|
requireUpdate := kube.ResourceList{}
|
|
|
|
|
|
|
|
|
|
|
|
err = resources.Visit(func(info *resource.Info, err error) error {
|
|
|
|
err := resources.Visit(func(info *resource.Info, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -67,14 +53,11 @@ func existingResourceConflict(resources kube.ResourceList, release string) (kube
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Allow adoption of the resource if it already has app.kubernetes.io/instance and app.kubernetes.io/managed-by labels.
|
|
|
|
// Allow adoption of the resource if it already has app.kubernetes.io/instance and app.kubernetes.io/managed-by labels.
|
|
|
|
lbls, err := accessor.Labels(existing)
|
|
|
|
anno, err := accessor.Annotations(existing)
|
|
|
|
if err == nil {
|
|
|
|
if err == nil && anno != nil && anno[helmReleaseNameAnnotation] == releaseName && anno[helmReleaseNamespaceAnnotation] == releaseNamespace {
|
|
|
|
set := labels.Set(lbls)
|
|
|
|
|
|
|
|
if sel.Matches(set) {
|
|
|
|
|
|
|
|
requireUpdate = append(requireUpdate, info)
|
|
|
|
requireUpdate = append(requireUpdate, info)
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
return fmt.Errorf(
|
|
|
|
"existing resource conflict: namespace: %s, name: %s, existing_kind: %s, new_kind: %s",
|
|
|
|
"existing resource conflict: namespace: %s, name: %s, existing_kind: %s, new_kind: %s",
|
|
|
@ -83,3 +66,21 @@ func existingResourceConflict(resources kube.ResourceList, release string) (kube
|
|
|
|
|
|
|
|
|
|
|
|
return requireUpdate, err
|
|
|
|
return requireUpdate, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func setMetadataVisitor(releaseName, releaseNamespace string) resource.VisitorFunc {
|
|
|
|
|
|
|
|
return func(info *resource.Info, err error) error {
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
anno, err := accessor.Annotations(info.Object)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if anno == nil {
|
|
|
|
|
|
|
|
anno = make(map[string]string)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
anno[helmReleaseNameAnnotation] = releaseName
|
|
|
|
|
|
|
|
anno[helmReleaseNamespaceAnnotation] = releaseNamespace
|
|
|
|
|
|
|
|
return accessor.SetAnnotations(info.Object, anno)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|