Move FileAssets into the NodeupAuxConfig

This commit is contained in:
John Gardiner Myers 2020-06-06 23:52:52 -07:00
parent 4bf9150ab6
commit 59c8826b17
4 changed files with 17 additions and 94 deletions

View File

@ -23,7 +23,6 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
)
@ -37,17 +36,6 @@ func b(v bool) *bool {
return fi.Bool(v)
}
// containsRole checks if a collection roles contains role v
func containsRole(v kops.InstanceGroupRole, list []kops.InstanceGroupRole) bool {
for _, x := range list {
if v == x {
return true
}
}
return false
}
// buildDockerEnvironmentVars just converts a series of keypairs to docker environment variables switches
func buildDockerEnvironmentVars(env map[string]string) []string {
var list []string

View File

@ -45,28 +45,12 @@ func (f *FileAssetsBuilder) Build(c *fi.ModelBuilderContext) error {
Mode: s("0755"),
})
// do we have any instanceGroup file assets
if f.InstanceGroup.Spec.FileAssets != nil {
if err := f.buildFileAssets(c, f.InstanceGroup.Spec.FileAssets, tracker); err != nil {
return err
}
}
if f.Cluster.Spec.FileAssets != nil {
if err := f.buildFileAssets(c, f.Cluster.Spec.FileAssets, tracker); err != nil {
return err
}
}
return nil
return f.buildFileAssets(c, f.NodeupAuxConfig.FileAssets, tracker)
}
// buildFileAssets is responsible for rendering the file assets to disk
func (f *FileAssetsBuilder) buildFileAssets(c *fi.ModelBuilderContext, assets []kops.FileAssetSpec, tracker map[string]bool) error {
for _, asset := range assets {
// @check if the file asset applies to us. If no roles applied we assume its applied to all roles
if len(asset.Roles) > 0 && !containsRole(f.NodeupConfig.InstanceGroupRole, asset.Roles) {
continue
}
// @check if e have a path and if not use the default path
assetPath := asset.Path
if assetPath == "" {

View File

@ -73,6 +73,8 @@ type Config struct {
// AuxConfig is the configuration for the nodeup binary that might be too big to fit in userdata.
type AuxConfig struct {
// FileAssets are a collection of file assets for this instance group.
FileAssets []kops.FileAssetSpec `json:",omitempty"`
// Hooks are for custom actions, for example on first installation.
Hooks [][]kops.HookSpec
}
@ -119,7 +121,8 @@ func NewConfig(cluster *kops.Cluster, instanceGroup *kops.InstanceGroup) (*Confi
igHooks := filterHooks(instanceGroup.Spec.Hooks, instanceGroup.Spec.Role)
auxConfig := AuxConfig{
Hooks: [][]kops.HookSpec{igHooks, clusterHooks},
FileAssets: append(filterFileAssets(instanceGroup.Spec.FileAssets, role), filterFileAssets(cluster.Spec.FileAssets, role)...),
Hooks: [][]kops.HookSpec{igHooks, clusterHooks},
}
if isMaster {
@ -156,6 +159,18 @@ func NewConfig(cluster *kops.Cluster, instanceGroup *kops.InstanceGroup) (*Confi
return &config, &auxConfig
}
func filterFileAssets(f []kops.FileAssetSpec, role kops.InstanceGroupRole) []kops.FileAssetSpec {
var fileAssets []kops.FileAssetSpec
for _, fileAsset := range f {
if len(fileAsset.Roles) > 0 && !containsRole(role, fileAsset.Roles) {
continue
}
fileAsset.Roles = nil
fileAssets = append(fileAssets, fileAsset)
}
return fileAssets
}
func filterHooks(h []kops.HookSpec, role kops.InstanceGroupRole) []kops.HookSpec {
var hooks []kops.HookSpec
for _, hook := range h {

View File

@ -19,7 +19,6 @@ package model
import (
"bytes"
"compress/gzip"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
@ -371,14 +370,6 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
}
}
fileAssets, err := b.getRelevantFileAssets(cs.FileAssets, b.ig.Spec.Role)
if err != nil {
return "", err
}
if len(fileAssets) > 0 {
spec["fileAssets"] = fileAssets
}
content, err := yaml.Marshal(spec)
if err != nil {
return "", fmt.Errorf("error converting cluster spec to yaml for inclusion within bootstrap script: %v", err)
@ -389,14 +380,6 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
"IGSpec": func() (string, error) {
spec := make(map[string]interface{})
fileAssets, err := b.getRelevantFileAssets(b.ig.Spec.FileAssets, b.ig.Spec.Role)
if err != nil {
return "", err
}
if len(fileAssets) > 0 {
spec["fileAssets"] = fileAssets
}
content, err := yaml.Marshal(spec)
if err != nil {
return "", fmt.Errorf("error converting instancegroup spec to yaml for inclusion within bootstrap script: %v", err)
@ -433,53 +416,6 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
return nil
}
// getRelevantFileAssets returns a list of file assets to be applied to the
// instance group, with the Content fingerprinted to reduce size
func (b *BootstrapScript) getRelevantFileAssets(allFileAssets []kops.FileAssetSpec, role kops.InstanceGroupRole) ([]kops.FileAssetSpec, error) {
relevantFileAssets := []kops.FileAssetSpec{}
for _, fileAsset := range allFileAssets {
if len(fileAsset.Roles) == 0 {
relevantFileAssets = append(relevantFileAssets, fileAsset)
continue
}
for _, fileAssetRole := range fileAsset.Roles {
if role == fileAssetRole {
relevantFileAssets = append(relevantFileAssets, fileAsset)
break
}
}
}
fileAssets := []kops.FileAssetSpec{}
if len(relevantFileAssets) > 0 {
for _, fileAsset := range relevantFileAssets {
if fileAsset.Content != "" {
contentFingerprint, err := b.computeFingerprint(fileAsset.Content)
if err != nil {
return nil, err
}
fileAsset.Content = contentFingerprint + " (fingerprint)"
}
fileAsset.Roles = nil
fileAssets = append(fileAssets, fileAsset)
}
}
return fileAssets, nil
}
// computeFingerprint takes a string and returns a base64 encoded fingerprint
func (b *BootstrapScript) computeFingerprint(content string) (string, error) {
hasher := sha1.New()
if _, err := hasher.Write([]byte(content)); err != nil {
return "", fmt.Errorf("error computing fingerprint hash: %v", err)
}
return base64.StdEncoding.EncodeToString(hasher.Sum(nil)), nil
}
func (b *BootstrapScript) createProxyEnv(ps *kops.EgressProxySpec) string {
var buffer bytes.Buffer