Move Hooks into the NodeupAuxConfig

This commit is contained in:
John Gardiner Myers 2020-06-06 23:06:35 -07:00
parent c3c1aca3c1
commit 06658c9d13
4 changed files with 35 additions and 74 deletions

View File

@ -40,13 +40,9 @@ var _ fi.ModelBuilder = &HookBuilder{}
func (h *HookBuilder) Build(c *fi.ModelBuilderContext) error {
// we keep a list of hooks name so we can allow local instanceGroup hooks override the cluster ones
hookNames := make(map[string]bool)
for i, spec := range []*[]kops.HookSpec{&h.InstanceGroup.Spec.Hooks, &h.Cluster.Spec.Hooks} {
for j, hook := range *spec {
for i, spec := range h.NodeupAuxConfig.Hooks {
for j, hook := range spec {
isInstanceGroup := i == 0
// filter roles if required
if len(hook.Roles) > 0 && !containsRole(h.NodeupConfig.InstanceGroupRole, hook.Roles) {
continue
}
// I don't want to affect those whom are already using the hooks, so I'm going to try to keep the name for now
// i.e. use the default naming convention - kops-hook-<index>, only those using the Name or hooks in IG should alter

View File

@ -62,7 +62,7 @@ func (b *UpdateServiceBuilder) buildFlatcarSystemdService(c *fi.ModelBuilderCont
return
}
for _, spec := range [][]kops.HookSpec{b.InstanceGroup.Spec.Hooks, b.Cluster.Spec.Hooks} {
for _, spec := range b.NodeupAuxConfig.Hooks {
for _, hook := range spec {
if hook.Name == flatcarServiceName || hook.Name == flatcarServiceName+".service" {
klog.Infof("Detected kops Hook for '%s'; skipping creation", flatcarServiceName)

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 {
// Hooks are for custom actions, for example on first installation.
Hooks [][]kops.HookSpec
}
type ConfigServerOptions struct {
@ -113,6 +115,13 @@ func NewConfig(cluster *kops.Cluster, instanceGroup *kops.InstanceGroup) (*Confi
VolumeMounts: instanceGroup.Spec.VolumeMounts,
}
clusterHooks := filterHooks(cluster.Spec.Hooks, instanceGroup.Spec.Role)
igHooks := filterHooks(instanceGroup.Spec.Hooks, instanceGroup.Spec.Role)
auxConfig := AuxConfig{
Hooks: [][]kops.HookSpec{igHooks, clusterHooks},
}
if isMaster {
reflectutils.JSONMergeStruct(&config.KubeletConfig, cluster.Spec.MasterKubelet)
@ -144,5 +153,27 @@ func NewConfig(cluster *kops.Cluster, instanceGroup *kops.InstanceGroup) (*Confi
config.DefaultMachineType = fi.String(strings.Split(instanceGroup.Spec.MachineType, ",")[0])
}
return &config, &AuxConfig{}
return &config, &auxConfig
}
func filterHooks(h []kops.HookSpec, role kops.InstanceGroupRole) []kops.HookSpec {
var hooks []kops.HookSpec
for _, hook := range h {
if len(hook.Roles) > 0 && !containsRole(role, hook.Roles) {
continue
}
hook.Roles = nil
hooks = append(hooks, hook)
}
return hooks
}
func containsRole(v kops.InstanceGroupRole, list []kops.InstanceGroupRole) bool {
for _, x := range list {
if v == x {
return true
}
}
return false
}

View File

@ -371,14 +371,6 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
}
}
hooks, err := b.getRelevantHooks(cs.Hooks, b.ig.Spec.Role)
if err != nil {
return "", err
}
if len(hooks) > 0 {
spec["hooks"] = hooks
}
fileAssets, err := b.getRelevantFileAssets(cs.FileAssets, b.ig.Spec.Role)
if err != nil {
return "", err
@ -397,14 +389,6 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
"IGSpec": func() (string, error) {
spec := make(map[string]interface{})
hooks, err := b.getRelevantHooks(b.ig.Spec.Hooks, b.ig.Spec.Role)
if err != nil {
return "", err
}
if len(hooks) > 0 {
spec["hooks"] = hooks
}
fileAssets, err := b.getRelevantFileAssets(b.ig.Spec.FileAssets, b.ig.Spec.Role)
if err != nil {
return "", err
@ -449,56 +433,6 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
return nil
}
// getRelevantHooks returns a list of hooks to be applied to the instance group,
// with the Manifest and ExecContainer Commands fingerprinted to reduce size
func (b *BootstrapScript) getRelevantHooks(allHooks []kops.HookSpec, role kops.InstanceGroupRole) ([]kops.HookSpec, error) {
relevantHooks := []kops.HookSpec{}
for _, hook := range allHooks {
if len(hook.Roles) == 0 {
relevantHooks = append(relevantHooks, hook)
continue
}
for _, hookRole := range hook.Roles {
if role == hookRole {
relevantHooks = append(relevantHooks, hook)
break
}
}
}
hooks := []kops.HookSpec{}
if len(relevantHooks) > 0 {
for _, hook := range relevantHooks {
if hook.Manifest != "" {
manifestFingerprint, err := b.computeFingerprint(hook.Manifest)
if err != nil {
return nil, err
}
hook.Manifest = manifestFingerprint + " (fingerprint)"
}
if hook.ExecContainer != nil && hook.ExecContainer.Command != nil {
execContainerCommandFingerprint, err := b.computeFingerprint(strings.Join(hook.ExecContainer.Command[:], " "))
if err != nil {
return nil, err
}
execContainerAction := &kops.ExecContainerAction{
Command: []string{execContainerCommandFingerprint + " (fingerprint)"},
Environment: hook.ExecContainer.Environment,
Image: hook.ExecContainer.Image,
}
hook.ExecContainer = execContainerAction
}
hook.Roles = nil
hooks = append(hooks, hook)
}
}
return hooks, 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) {