github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/tiller/resource_policy.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tiller
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  
    23  	"k8s.io/helm/pkg/kube"
    24  	"k8s.io/helm/pkg/tiller/environment"
    25  )
    26  
    27  // resourcePolicyAnno is the annotation name for a resource policy
    28  const resourcePolicyAnno = "helm.sh/resource-policy"
    29  
    30  // keepPolicy is the resource policy type for keep
    31  //
    32  // This resource policy type allows resources to skip being deleted
    33  //   during an uninstallRelease action.
    34  const keepPolicy = "keep"
    35  
    36  func filterManifestsToKeep(manifests []Manifest) ([]Manifest, []Manifest) {
    37  	remaining := []Manifest{}
    38  	keep := []Manifest{}
    39  
    40  	for _, m := range manifests {
    41  		if m.Head.Metadata == nil || m.Head.Metadata.Annotations == nil || len(m.Head.Metadata.Annotations) == 0 {
    42  			remaining = append(remaining, m)
    43  			continue
    44  		}
    45  
    46  		resourcePolicyType, ok := m.Head.Metadata.Annotations[resourcePolicyAnno]
    47  		if !ok {
    48  			remaining = append(remaining, m)
    49  			continue
    50  		}
    51  
    52  		resourcePolicyType = strings.ToLower(strings.TrimSpace(resourcePolicyType))
    53  		if resourcePolicyType == keepPolicy {
    54  			keep = append(keep, m)
    55  		}
    56  
    57  	}
    58  	return keep, remaining
    59  }
    60  
    61  func summarizeKeptManifests(manifests []Manifest, kubeClient environment.KubeClient, namespace string) string {
    62  	var message string
    63  	for _, m := range manifests {
    64  		// check if m is in fact present from k8s client's POV.
    65  		output, err := kubeClient.Get(namespace, bytes.NewBufferString(m.Content))
    66  		if err != nil || strings.Contains(output, kube.MissingGetHeader) {
    67  			continue
    68  		}
    69  
    70  		details := "[" + m.Head.Kind + "] " + m.Head.Metadata.Name + "\n"
    71  		if message == "" {
    72  			message = "These resources were kept due to the resource policy:\n"
    73  		}
    74  		message = message + details
    75  	}
    76  	return message
    77  }