github.com/diafour/helm@v3.0.0-beta.3+incompatible/pkg/chart/chart.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package chart
    17  
    18  import "strings"
    19  
    20  // APIVersionV1 is the API version number for version 1.
    21  const APIVersionV1 = "v1"
    22  
    23  // APIVersionV2 is the API version number for version 2.
    24  const APIVersionV2 = "v2"
    25  
    26  // Chart is a helm package that contains metadata, a default config, zero or more
    27  // optionally parameterizable templates, and zero or more charts (dependencies).
    28  type Chart struct {
    29  	// Metadata is the contents of the Chartfile.
    30  	Metadata *Metadata
    31  	// LocK is the contents of Chart.lock.
    32  	Lock *Lock
    33  	// Templates for this chart.
    34  	Templates []*File
    35  	// Values are default config for this template.
    36  	Values map[string]interface{}
    37  	// Schema is an optional JSON schema for imposing structure on Values
    38  	Schema []byte
    39  	// Files are miscellaneous files in a chart archive,
    40  	// e.g. README, LICENSE, etc.
    41  	Files []*File
    42  
    43  	parent       *Chart
    44  	dependencies []*Chart
    45  }
    46  
    47  // SetDependencies replaces the chart dependencies.
    48  func (ch *Chart) SetDependencies(charts ...*Chart) {
    49  	ch.dependencies = nil
    50  	ch.AddDependency(charts...)
    51  }
    52  
    53  // Name returns the name of the chart.
    54  func (ch *Chart) Name() string {
    55  	if ch.Metadata == nil {
    56  		return ""
    57  	}
    58  	return ch.Metadata.Name
    59  }
    60  
    61  // AddDependency determines if the chart is a subchart.
    62  func (ch *Chart) AddDependency(charts ...*Chart) {
    63  	for i, x := range charts {
    64  		charts[i].parent = ch
    65  		ch.dependencies = append(ch.dependencies, x)
    66  	}
    67  }
    68  
    69  // Root finds the root chart.
    70  func (ch *Chart) Root() *Chart {
    71  	if ch.IsRoot() {
    72  		return ch
    73  	}
    74  	return ch.Parent().Root()
    75  }
    76  
    77  // Dependencies are the charts that this chart depends on.
    78  func (ch *Chart) Dependencies() []*Chart { return ch.dependencies }
    79  
    80  // IsRoot determines if the chart is the root chart.
    81  func (ch *Chart) IsRoot() bool { return ch.parent == nil }
    82  
    83  // Parent returns a subchart's parent chart.
    84  func (ch *Chart) Parent() *Chart { return ch.parent }
    85  
    86  // ChartPath returns the full path to this chart in dot notation.
    87  func (ch *Chart) ChartPath() string {
    88  	if !ch.IsRoot() {
    89  		return ch.Parent().ChartPath() + "." + ch.Name()
    90  	}
    91  	return ch.Name()
    92  }
    93  
    94  // ChartFullPath returns the full path to this chart.
    95  func (ch *Chart) ChartFullPath() string {
    96  	if !ch.IsRoot() {
    97  		return ch.Parent().ChartFullPath() + "/charts/" + ch.Name()
    98  	}
    99  	return ch.Name()
   100  }
   101  
   102  // Validate validates the metadata.
   103  func (ch *Chart) Validate() error {
   104  	return ch.Metadata.Validate()
   105  }
   106  
   107  // AppVersion returns the appversion of the chart.
   108  func (ch *Chart) AppVersion() string {
   109  	if ch.Metadata == nil {
   110  		return ""
   111  	}
   112  	return ch.Metadata.AppVersion
   113  }
   114  
   115  // CRDs returns a list of File objects in the 'crds/' directory of a Helm chart.
   116  func (ch *Chart) CRDs() []*File {
   117  	files := []*File{}
   118  	// Find all resources in the crds/ directory
   119  	for _, f := range ch.Files {
   120  		if strings.HasPrefix(f.Name, "crds/") {
   121  			files = append(files, f)
   122  		}
   123  	}
   124  	// Get CRDs from dependencies, too.
   125  	for _, dep := range ch.Dependencies() {
   126  		files = append(files, dep.CRDs()...)
   127  	}
   128  	return files
   129  }