-
Notifications
You must be signed in to change notification settings - Fork 93
Apply additional kube manifests from configs to cluster #817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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" | ||
"github.com/k0sproject/rig/exec" | ||
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 { | ||
if !p.IsWet() { | ||
p.DryMsgf(p.leader, "apply manifest %s (%d bytes)", name, len(content)) | ||
return nil | ||
} | ||
|
||
log.Infof("%s: apply manifest %s (%d bytes)", p.leader, name, len(content)) | ||
kubectlCmd := p.leader.Configurer.KubectlCmdf(p.leader, p.leader.K0sDataDir(), "apply -f -") | ||
var stdout, stderr bytes.Buffer | ||
|
||
cmd, err := p.leader.ExecStreams(kubectlCmd, io.NopCloser(bytes.NewReader(content)), &stdout, &stderr, exec.Sudo(p.leader)) | ||
if err != nil { | ||
return fmt.Errorf("failed to run apply for manifest %s: %w", name, err) | ||
} | ||
if err := cmd.Wait(); err != nil { | ||
log.Errorf("%s: kubectl apply failed for manifest %s", p.leader, name) | ||
log.Errorf("%s: kubectl apply stderr: %s", p.leader, stderr.String()) | ||
} | ||
log.Infof("%s: kubectl apply: %s", p.leader, stdout.String()) | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't we loading all
otherConfigs
from a single yaml doc? In this case, what doesotherConfig.Filename()
refer to?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k0sctl accepts multiple
--config
s or a dir--config configs/
(which gets converted toconfigs/**/*.{yml,yaml}
or a glob pattern--config "configs/k0s*.yaml"
, not only a single multidoc yaml.The manifest reader tries to keep track of their origins for displaying messages, but if it came from stdin or it doesn't know the filename for some other reason, it either just calls it "manifest" (during preliminary reading/unmarshaling) or calls it
fmt.Sprintf("%s-%s-%d.yaml", safeFn(rd.APIVersion), safeFn(rd.Kind), time.Now().UnixNano())
(once the header has been parsed).