-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1380 from facchettos/k0s-in-syncer
k0s is now run from the syncer container
- Loading branch information
Showing
6 changed files
with
134 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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,73 @@ | ||
package k0s | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/ghodss/yaml" | ||
"github.com/loft-sh/log/scanner" | ||
"github.com/loft-sh/vcluster/pkg/util/loghelper" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const VClusterCommandEnv = "VCLUSTER_COMMAND" | ||
|
||
type k0sCommand struct { | ||
Command []string `json:"command,omitempty"` | ||
Args []string `json:"args,omitempty"` | ||
} | ||
|
||
func StartK0S(ctx context.Context) error { | ||
reader, writer, err := os.Pipe() | ||
if err != nil { | ||
return err | ||
} | ||
defer writer.Close() | ||
|
||
command := &k0sCommand{} | ||
err = yaml.Unmarshal([]byte(os.Getenv(VClusterCommandEnv)), command) | ||
if err != nil { | ||
return fmt.Errorf("parsing k0s command %s: %w", os.Getenv(VClusterCommandEnv), err) | ||
} | ||
|
||
args := append(command.Command, command.Args...) | ||
|
||
// start func | ||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
|
||
// make sure we scan the output correctly | ||
scan := scanner.NewScanner(reader) | ||
for scan.Scan() { | ||
line := scan.Text() | ||
if len(line) == 0 { | ||
continue | ||
} | ||
|
||
// print to our logs | ||
args := []interface{}{"component", "k0s"} | ||
loghelper.PrintKlogLine(line, args) | ||
} | ||
}() | ||
|
||
// start the command | ||
klog.InfoS("Starting k0s", "args", strings.Join(args, " ")) | ||
cmd := exec.CommandContext(ctx, args[0], args[1:]...) | ||
cmd.Stdout = writer | ||
cmd.Stderr = writer | ||
err = cmd.Run() | ||
|
||
// make sure we wait for scanner to be done | ||
_ = writer.Close() | ||
<-done | ||
|
||
// regular stop case | ||
if err != nil && err.Error() != "signal: killed" { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains 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 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 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