Skip to content

Commit

Permalink
Apply extra manifests
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Jan 13, 2025
1 parent c23f154 commit 36af5f7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions action/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func NewApply(opts ApplyOptions) *Apply {
&phase.ResetWorkers{NoDrain: opts.NoDrain},
&phase.ResetControllers{NoDrain: opts.NoDrain},
&phase.RunHooks{Stage: "after", Action: "apply"},
&phase.ApplyManifests{},
unlockPhase,
&phase.Disconnect{},
},
Expand Down
63 changes: 63 additions & 0 deletions phase/apply_manifests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package phase

import (
"bytes"
"fmt"
"io"

"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
log "github.com/sirupsen/logrus"
)

// ApplyManifests is a phase that applies additional manifests to the cluster
type ApplyManifests struct {
GenericPhase
leader *cluster.Host
}

// Title for the phase
func (p *ApplyManifests) Title() string {
return "Apply additional manifests"
}

// Prepare the phase
func (p *ApplyManifests) Prepare(config *v1beta1.Cluster) error {
p.Config = config
p.leader = p.Config.Spec.K0sLeader()

return nil
}

// ShouldRun is true when there are additional manifests to apply
func (p *ApplyManifests) ShouldRun() bool {
return len(p.Config.Metadata.Manifests) > 0
}

// Run the phase
func (p *ApplyManifests) Run() error {
for name, content := range p.Config.Metadata.Manifests {
if err := p.apply(name, content); err != nil {
return err
}
}

return nil
}

func (p *ApplyManifests) apply(name string, content []byte) error {
kubectlCmd := p.leader.Configurer.KubectlCmdf(p.leader, "apply", "-f", "-")
var stdout, stderr bytes.Buffer

cmd, err := p.leader.ExecStreams(kubectlCmd, io.NopCloser(bytes.NewReader(content)), &stdout, &stderr)
if err != nil {
return fmt.Errorf("failed to run apply for manifest %s: %w", name, err)
}
if err := cmd.Wait(); err != nil {
log.Errorf("kubectl apply failed for manifest %s", name)
log.Errorf("kubectl apply stdout: %s", stdout.String())
log.Errorf("kubectl apply stderr: %s", stderr.String())
return fmt.Errorf("failed to apply manifest %s: %w", name, err)
}
return nil
}

0 comments on commit 36af5f7

Please sign in to comment.