github.com/replicatedhq/ship@v0.55.0/pkg/api/lifecycle.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/replicatedhq/ship/pkg/constants"
     8  )
     9  
    10  // A Lifecycle  is the top-level lifecycle object
    11  type Lifecycle struct {
    12  	V1 []Step `json:"v1,omitempty" yaml:"v1,omitempty" hcl:"v1,omitempty"`
    13  }
    14  
    15  // Step represents vendor-customized configuration steps & messaging
    16  type Step struct {
    17  	Message        *Message        `json:"message,omitempty" yaml:"message,omitempty" hcl:"message,omitempty"`
    18  	Config         *ConfigStep     `json:"config,omitempty" yaml:"config,omitempty" hcl:"config,omitempty"`
    19  	Render         *Render         `json:"render,omitempty" yaml:"render,omitempty" hcl:"render,omitempty"`
    20  	Terraform      *Terraform      `json:"terraform,omitempty" yaml:"terraform,omitempty" hcl:"terraform,omitempty"`
    21  	Kustomize      *Kustomize      `json:"kustomize,omitempty" yaml:"kustomize,omitempty" hcl:"kustomize,omitempty"`
    22  	Unfork         *Unfork         `json:"unfork,omitempty" yaml:"unfork,omitempty" hcl:"unfork,omitempty"`
    23  	KustomizeIntro *KustomizeIntro `json:"kustomizeIntro,omitempty" yaml:"kustomizeIntro,omitempty" hcl:"kustomizeIntro,omitempty"`
    24  	HelmIntro      *HelmIntro      `json:"helmIntro,omitempty" yaml:"helmIntro,omitempty" hcl:"helmIntro,omitempty"`
    25  	HelmValues     *HelmValues     `json:"helmValues,omitempty" yaml:"helmValues,omitempty" hcl:"helmValues,omitempty"`
    26  	KubectlApply   *KubectlApply   `json:"kubectlApply,omitempty" yaml:"kubectlApply,omitempty" hcl:"kubectlApply,omitempty"`
    27  }
    28  
    29  func (s *Step) String() string {
    30  	step := s.GetStep()
    31  	return fmt.Sprintf("api.Step{ID: %q, Name: %q}", step.Shared().ID, step.ShortName())
    32  }
    33  
    34  type StepDetails interface {
    35  	Shared() *StepShared
    36  	ShortName() string
    37  }
    38  
    39  func (s Step) GetStep() StepDetails {
    40  	if s.Message != nil {
    41  		return s.Message
    42  
    43  	} else if s.Render != nil {
    44  		return s.Render
    45  	} else if s.Config != nil {
    46  		return s.Config
    47  	} else if s.Terraform != nil {
    48  		return s.Terraform
    49  	} else if s.KustomizeIntro != nil {
    50  		return s.KustomizeIntro
    51  	} else if s.Kustomize != nil {
    52  		return s.Kustomize
    53  	} else if s.Unfork != nil {
    54  		return s.Unfork
    55  	} else if s.HelmIntro != nil {
    56  		return s.HelmIntro
    57  	} else if s.HelmValues != nil {
    58  		return s.HelmValues
    59  	} else if s.KubectlApply != nil {
    60  		return s.KubectlApply
    61  	}
    62  	return nil
    63  }
    64  func (s Step) Shared() *StepShared { return s.GetStep().Shared() }
    65  func (s Step) ShortName() string   { return s.GetStep().ShortName() }
    66  
    67  type StepShared struct {
    68  	ID          string   `json:"id,omitempty" yaml:"id,omitempty" hcl:",key"`
    69  	Description string   `json:"description,omitempty" yaml:"description,omitempty" hcl:"description,omitempty"`
    70  	Requires    []string `json:"requires,omitempty" yaml:"requires,omitempty" hcl:"requires,omitempty"`
    71  	Invalidates []string `json:"invalidates,omitempty" yaml:"invalidates,omitempty" hcl:"invalidates,omitempty"`
    72  }
    73  
    74  // Message is a lifeycle step to print a message
    75  type Message struct {
    76  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
    77  	Contents   string `json:"contents" yaml:"contents" hcl:"contents"`
    78  	Level      string `json:"level,omitempty" yaml:"level,omitempty" hcl:"level,omitempty"`
    79  }
    80  
    81  func (m *Message) Shared() *StepShared { return &m.StepShared }
    82  func (m *Message) ShortName() string   { return "message" }
    83  
    84  // Render is a lifeycle step to collect config and render assets
    85  type Render struct {
    86  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
    87  	Root       string  `json:"root,omitempty" yaml:"root,omitempty" hcl:"root,omitempty"`
    88  	Assets     *Assets `json:"assets,omitempty" yaml:"assets,omitempty" hcl:"assets,omitempty"`
    89  }
    90  
    91  func (r *Render) Shared() *StepShared { return &r.StepShared }
    92  
    93  func (r *Render) ShortName() string { return "render" }
    94  func (r *Render) RenderRoot() string {
    95  	if r.Root == "" {
    96  		return constants.InstallerPrefixPath
    97  	}
    98  	return r.Root
    99  }
   100  
   101  // Terraform is a lifeycle step to execute `apply` for a runbook's terraform asset
   102  type Terraform struct {
   103  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   104  	Path       string `json:"path,omitempty" yaml:"path,omitempty" hcl:"path,omitempty"`
   105  	When       string `json:"when,omitempty" yaml:"when,omitempty" hcl:"when,omitempty"`
   106  }
   107  
   108  func (t *Terraform) Shared() *StepShared { return &t.StepShared }
   109  func (t *Terraform) ShortName() string   { return "terraform" }
   110  
   111  // Unfork is a lifecycle step to generate patches and overlays for
   112  // two generates assets that consist of raw K8S YAML
   113  type Unfork struct {
   114  	StepShared   `json:",inline" yaml:",inline" hcl:",inline"`
   115  	UpstreamBase string `json:"upstreamBase" yaml:"upstreamBase" hcl:"upstreamBase"`
   116  	ForkedBase   string `json:"forkedBase" yaml:"forkedBase" hcl:"forkedBase"`
   117  	Dest         string `json:"dest,omitempty" yaml:"dest,omitempty" hcl:"dest,omitempty"`
   118  	Overlay      string `json:"overlay,omitempty" yaml:"overlay,omitempty" hcl:"overlay,omitempty"`
   119  }
   120  
   121  func (k *Unfork) OverlayPath() string {
   122  	if k.Overlay == "" {
   123  		return "overlays/ship"
   124  	}
   125  	return k.Overlay
   126  }
   127  
   128  func (u *Unfork) Shared() *StepShared { return &u.StepShared }
   129  func (k *Unfork) ShortName() string   { return "unfork" }
   130  
   131  // Kustomize is a lifecycle step to generate overlays for generated assets.
   132  // It does not take a kustomization.yml, rather it will generate one in the .ship/ folder
   133  type Kustomize struct {
   134  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   135  	Base       string `json:"base,omitempty" yaml:"base,omitempty" hcl:"base,omitempty"`
   136  	Dest       string `json:"dest,omitempty" yaml:"dest,omitempty" hcl:"dest,omitempty"`
   137  	Overlay    string `json:"overlay,omitempty" yaml:"overlay,omitempty" hcl:"overlay,omitempty"`
   138  }
   139  
   140  func (k *Kustomize) OverlayPath() string {
   141  	if k.Overlay == "" {
   142  		return "overlays/ship"
   143  	}
   144  	return k.Overlay
   145  }
   146  
   147  func (k *Kustomize) Shared() *StepShared    { return &k.StepShared }
   148  func (k *Kustomize) ShortName() string      { return "kustomize" }
   149  func (k *Kustomize) TempRenderPath() string { return filepath.Join(constants.KustomizeRenderPath, k.ID) }
   150  
   151  // KustomizeIntro is a lifeycle step to display an informative intro page for kustomize
   152  type KustomizeIntro struct {
   153  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   154  }
   155  
   156  func (k *KustomizeIntro) Shared() *StepShared { return &k.StepShared }
   157  func (k *KustomizeIntro) ShortName() string   { return "kustomize-intro" }
   158  
   159  // HelmIntro is a lifecycle step to render persisted README.md in the .ship folder
   160  type HelmIntro struct {
   161  	IsUpdate   bool
   162  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   163  }
   164  
   165  func (h *HelmIntro) Shared() *StepShared { return &h.StepShared }
   166  func (h *HelmIntro) ShortName() string   { return "helm-intro" }
   167  
   168  // HelmValues is a lifecycle step to render persisted values.yaml in the .ship folder
   169  // and save user input changes to values.yaml
   170  type HelmValues struct {
   171  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   172  	Path       string                  `json:"path,omitempty" yaml:"path,omitempty" hcl:"path,omitempty"`
   173  	Readme     *HelmValuesReadmeSource `json:"readme,omitempty" yaml:"readme,omitempty" hcl:"readme,omitempty"`
   174  }
   175  
   176  // A HelmValuesReadmeSource tells ship how to populate the readme file for the helm values editor.
   177  // if not provided, ship will use the default behavior of pulling the readme from the
   178  // root of the default target chart
   179  type HelmValuesReadmeSource struct {
   180  	Contents string `json:"contents,omitempty" yaml:"contents,omitempty" hcl:"contents,omitempty"`
   181  	// someday we'll support file or something too
   182  }
   183  
   184  func (h *HelmValues) Shared() *StepShared { return &h.StepShared }
   185  func (h *HelmValues) ShortName() string   { return "helm-values" }
   186  
   187  type ConfigStep struct {
   188  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   189  }
   190  
   191  func (c *ConfigStep) Shared() *StepShared {
   192  	return &c.StepShared
   193  }
   194  
   195  func (c ConfigStep) ShortName() string {
   196  	return "config"
   197  }
   198  
   199  // KubectlApply is a lifeycle step to execute `apply` for a kubeconfig asset
   200  type KubectlApply struct {
   201  	StepShared `json:",inline" yaml:",inline" hcl:",inline"`
   202  	Path       string `json:"path,omitempty" yaml:"path,omitempty" hcl:"path,omitempty"`
   203  	Kubeconfig string `json:"kubeconfig,omitempty" yaml:"kubeconfig,omitempty" hcl:"kubeconfig,omitempty"`
   204  }
   205  
   206  func (k *KubectlApply) Shared() *StepShared { return &k.StepShared }
   207  func (k *KubectlApply) ShortName() string   { return "kubectl" }