github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/plugin.go (about)

     1  package api
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/kubernetes-incubator/kube-aws/provisioner"
     9  )
    10  
    11  // A plugin consists of two parts: a set of metadata and a spec
    12  type Plugin struct {
    13  	Metadata `yaml:"metadata,omitempty"`
    14  	Spec     PluginSpec `yaml:"spec,omitempty"`
    15  }
    16  
    17  func (p Plugin) EnabledIn(plugins PluginConfigs) (bool, *PluginConfig) {
    18  	for name, c := range plugins {
    19  		if name == p.SettingKey() && c.Enabled {
    20  			return true, &c
    21  		}
    22  	}
    23  	return false, nil
    24  }
    25  
    26  func (p Plugin) Validate() error {
    27  	if err := p.Metadata.Validate(); err != nil {
    28  		return fmt.Errorf("Invalid metadata: %v", err)
    29  	}
    30  	return nil
    31  }
    32  
    33  func (p Plugin) SettingKey() string {
    34  	words := strings.Split(p.Metadata.Name, "-")
    35  	for i, _ := range words {
    36  		if i > 0 {
    37  			words[i] = strings.Title(words[i])
    38  		}
    39  	}
    40  	return strings.Join(words, "")
    41  }
    42  
    43  // Metadata is the metadata of a kube-aws plugin consists of various settings specific to the plugin itself
    44  // Metadata never affects what are injected into K8S clusters, node, other CFN resources managed by kube-aws.
    45  type Metadata struct {
    46  	Name        string `yaml:"name"`
    47  	Version     string `yaml:"version"`
    48  	Description string `yaml:"description"`
    49  	// ClusterSettingsKey is the key in the root of cluster.yaml used for configuring this plugin cluster-wide
    50  	ClusterSettingsKey string `yaml:"clusterSettingsKey,omitempty"`
    51  	// NodePoolSettingsKey is the key in the root of a node pool settings in cluster.yaml used for configuring this plugin only for a node pool
    52  	NodePoolSettingsKey string `yaml:"nodePoolSettingKey,omitempty"`
    53  }
    54  
    55  func (m Metadata) Validate() error {
    56  	if m.Name == "" {
    57  		return errors.New("`name` must not be empty")
    58  	}
    59  	if m.Version == "" {
    60  		return errors.New("`version` must not be empty")
    61  	}
    62  	return nil
    63  }
    64  
    65  // PluginSpec is the specification of a kube-aws plugin
    66  // A spec consists of two parts: Cluster and Command
    67  type PluginSpec struct {
    68  	// Cluster is the configuration part of a plugin which is used to append arbitrary configs into various resources managed by kube-aws
    69  	Cluster ClusterSpec `yaml:"cluster,omitempty"`
    70  }
    71  
    72  // Cluster is the configuration part of a plugin which is used to append arbitrary configs into various resources managed by kube-aws
    73  type ClusterSpec struct {
    74  	// Values represents the values available in templates
    75  	Values `yaml:"values,omitempty"`
    76  	// CloudFormation represents customizations to CloudFormation-related settings and configurations
    77  	CloudFormation CloudFormationSpec `yaml:"cloudformation,omitempty"`
    78  	// Helm represents what are injected into the resulting K8S cluster via Helm - a package manager for K8S
    79  	Helm `yaml:"helm,omitempty"`
    80  	// Kubernetes represents what are injected into the resulting K8S
    81  	Kubernetes Kubernetes `yaml:"kubernetes,omitempty"`
    82  	// Machine represents what are injected into each machines managed by kube-aws
    83  	Machine `yaml:"machine,omitempty"`
    84  	// PKI extends the cluster PKI managed by kube-aws
    85  	PKI `yaml:"pki,omitempty"`
    86  }
    87  
    88  // CloudFormation represents customizations to CloudFormation-related settings and configurations
    89  type CloudFormationSpec struct {
    90  	Stacks `yaml:"stacks,omitempty"`
    91  }
    92  
    93  type Stacks struct {
    94  	Root         Stack `yaml:"root,omitempty"`
    95  	Network      Stack `yaml:"network,omitempty"`
    96  	ControlPlane Stack `yaml:"controlPlane,omitempty"`
    97  	Etcd         Stack `yaml:"etcd,omitempty"`
    98  	NodePool     Stack `yaml:"nodePool,omitempty"`
    99  }
   100  
   101  // Stack represents a set of customizations to a CloudFormation stack template
   102  // Top-level keys should be one of: Resources, Outputs
   103  // Second-level keys should be cfn resource names
   104  type Stack struct {
   105  	Resources `yaml:"resources,omitempty"`
   106  	Outputs   `yaml:"outputs,omitempty"`
   107  	Tags      `yaml:"tags,omitempty"`
   108  }
   109  
   110  type Resources struct {
   111  	provisioner.RemoteFileSpec `yaml:",inline"`
   112  }
   113  
   114  type Outputs struct {
   115  	provisioner.RemoteFileSpec `yaml:",inline"`
   116  }
   117  
   118  type Tags struct {
   119  	provisioner.RemoteFileSpec `yaml:",inline"`
   120  }
   121  
   122  type Helm struct {
   123  	// Releases is a list of helm releases to be maintained on the cluster.
   124  	// Note that the list is sorted by their names by kube-aws so that it won't result in unnecessarily node replacements.
   125  	Releases HelmReleases `yaml:"releases,omitempty"`
   126  }
   127  
   128  //func (k *Helm) UnmarshalYAML(unmarshal func(interface{}) error) error {
   129  //	type t Helm
   130  //	work := t(Helm{
   131  //		Releases: HelmReleases{},
   132  //	})
   133  //	if err := unmarshal(&work); err != nil {
   134  //		return fmt.Errorf("failed to parse helm plugin config: %v", err)
   135  //	}
   136  //	*k = Helm(work)
   137  //
   138  //	return nil
   139  //}
   140  //
   141  type HelmReleases []HelmRelease
   142  
   143  type HelmRelease struct {
   144  	Name    string `yaml:"name,omitempty"`
   145  	Chart   string `yaml:"chart,omitempty"`
   146  	Version string `yaml:"version,omitempty"`
   147  	Values  Values `yaml:"values,omitempty"`
   148  }
   149  
   150  type KubernetesAPIServer struct {
   151  	Flags   CommandLineFlags `yaml:"flags,omitempty"`
   152  	Volumes APIServerVolumes `yaml:"volumes,omitempty"`
   153  }
   154  
   155  type CommandLineFlags []CommandLineFlag
   156  
   157  type CommandLineFlag struct {
   158  	// Name is the name of a command-line flag passed to the k8s apiserver.
   159  	// For example, a name 	is "oidc-issuer-url" for the flag `--oidc-issuer-url`.
   160  	Name string `yaml:"name,omitempty"`
   161  	// Value is a golang text template resulting to the value of a command-line flag passed to the k8s apiserver
   162  	Value string `yaml:"value,omitempty"`
   163  }
   164  
   165  type APIServerVolumes []APIServerVolume
   166  
   167  type APIServerVolume struct {
   168  	// Name is translated to both a volume mount's and volume's name
   169  	Name string `yaml:"name,omitempty"`
   170  	// Path is translated to both a volume mount's mountPath and a volume's hostPath
   171  	Path     string `yaml:"path,omitempty"`
   172  	ReadOnly bool   `yaml:"readOnly,omitempty"`
   173  }
   174  
   175  type KubernetesManifests []KubernetesManifest
   176  
   177  type KubernetesManifest struct {
   178  	Name                       string `yaml:"name,omitempty"`
   179  	provisioner.RemoteFileSpec `yaml:",inline"`
   180  }
   181  
   182  type Contents struct {
   183  	provisioner.RemoteFileSpec `yaml:",inline"`
   184  	// TODO Better naming
   185  	UnknownKeys map[string]interface{} `yaml:",inline"`
   186  }
   187  
   188  type Source struct {
   189  	Path string `yaml:"path,omitempty"`
   190  }
   191  
   192  type Machine struct {
   193  	Roles MachineRoles `yaml:"roles,omitempty"`
   194  }
   195  
   196  type MachineRoles struct {
   197  	Controller Node        `yaml:"controller,omitempty"`
   198  	Etcd       MachineSpec `yaml:"etcd,omitempty"`
   199  	Worker     Node        `yaml:"worker,omitempty"`
   200  }
   201  
   202  // Node is a worker machine in Kubernetes
   203  type Node struct {
   204  	MachineSpec `yaml:",inline"`
   205  	Kubelet     KubeletSpec `yaml:"kubelet,omitempty"`
   206  }
   207  
   208  type MachineSpec struct {
   209  	Files   `yaml:"files,omitempty"`
   210  	IAM     `yaml:"iam,omitempty"`
   211  	Systemd `yaml:"systemd,omitempty"`
   212  }
   213  
   214  type Files []provisioner.RemoteFileSpec
   215  
   216  type IAM struct {
   217  	Policy IAMPolicy `yaml:"policy,omitempty"`
   218  }
   219  
   220  type Systemd struct {
   221  	// Units is a list of systemd units installed on the nodes
   222  	Units SystemdUnits `yaml:"units,omitempty"`
   223  }
   224  
   225  type SystemdUnits []SystemdUnit
   226  
   227  type SystemdUnit struct {
   228  	Name string `yaml:"name,omitempty"`
   229  	// Contents must be a valid go text template producing a valid systemd unit definition
   230  	Contents `yaml:",inline"`
   231  }
   232  
   233  // Kubelet represents a set of customizations to kubelets running on the nodes
   234  // Keys must be included in: nodeLabels, featureGates, etc
   235  // kubelet can be configured per-node-pool-basic hence a part of WorkerSettings
   236  type KubeletSpec struct {
   237  	FeatureGates FeatureGates           `yaml:"featureGates,omitempty"`
   238  	NodeLabels   NodeLabels             `yaml:"nodeLabels,omitempty"`
   239  	Kubeconfig   string                 `yaml:"kubeconfig,omitempty"`
   240  	Mounts       []ContainerVolumeMount `yaml:"mounts,omitempty"`
   241  }
   242  
   243  type ContainerVolumeMount string
   244  
   245  func (m ContainerVolumeMount) ToRktRunArgs() []string {
   246  	args := []string{}
   247  	// Avoids invalid volname like "-opt-bin" for "/opt/bin" or "opt-bin-" for "opt/bin/". It should obviously be "opt-bin".
   248  	volname := strings.Replace(strings.TrimPrefix(strings.TrimSuffix(string(m), "/"), "/"), "/", "-", -1)
   249  	args = append(
   250  		args,
   251  		fmt.Sprintf("--mount volume=%s,target=%s", volname, string(m)),
   252  		fmt.Sprintf("--volume %s,kind=host,source=%s", volname, string(m)),
   253  	)
   254  	return args
   255  }
   256  
   257  func (m ContainerVolumeMount) MountDockerRW() string {
   258  	return fmt.Sprintf("-v %s:%s:rw", m, m)
   259  }
   260  
   261  type Values map[string]interface{}