-
Notifications
You must be signed in to change notification settings - Fork 407
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
Support loading into K3s with k3s.local #665
Open
kameshsampath
wants to merge
4
commits into
ko-build:main
Choose a base branch
from
kameshsampath:ko-k3s
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 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 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,114 @@ | ||
package publish | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/ko/pkg/build" | ||
"github.com/google/ko/pkg/publish/k3s" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
//K3sDomain k3s local sentinel registry where the images gets loaded | ||
// TODO(kamesh) handle namespaces as part of URI ?? | ||
K3sDomain = "k3s.local" | ||
) | ||
|
||
type k3sPublisher struct { | ||
namer Namer | ||
tags []string | ||
} | ||
|
||
//NewK3sPublisher returns a new publish.Interface that loads image into k3s clusters | ||
func NewK3sPublisher(namer Namer, tags []string) Interface { | ||
return &k3sPublisher{ | ||
namer: namer, | ||
tags: tags, | ||
} | ||
} | ||
|
||
//Publish implements publish.Interface | ||
func (k *k3sPublisher) Publish(ctx context.Context, br build.Result, s string) (name.Reference, error) { | ||
s = strings.TrimPrefix(s, build.StrictScheme) | ||
s = strings.ToLower(s) | ||
|
||
// There's no way to write an index to a kind, so attempt to downcast it to an image. | ||
var img v1.Image | ||
switch i := br.(type) { | ||
case v1.Image: | ||
img = i | ||
case v1.ImageIndex: | ||
im, err := i.IndexManifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
goos, goarch := os.Getenv("GOOS"), os.Getenv("GOARCH") | ||
if goos == "" { | ||
goos = "linux" | ||
} | ||
if goarch == "" { | ||
goarch = "amd64" | ||
} | ||
for _, manifest := range im.Manifests { | ||
if manifest.Platform == nil { | ||
continue | ||
} | ||
if manifest.Platform.OS != goos { | ||
continue | ||
} | ||
if manifest.Platform.Architecture != goarch { | ||
continue | ||
} | ||
img, err = i.Image(manifest.Digest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
break | ||
} | ||
if img == nil { | ||
return nil, fmt.Errorf("failed to find %s/%s image in index for image: %v", goos, goarch, s) | ||
} | ||
default: | ||
return nil, fmt.Errorf("failed to interpret %s result as image: %v", s, br) | ||
} | ||
|
||
h, err := img.Digest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
digestTag, err := name.NewTag(fmt.Sprintf("%s:%s", k.namer(K3sDomain, s), h.Hex)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Printf("Loading %v", digestTag) | ||
if err := k3s.Write(ctx, digestTag, img); err != nil { | ||
return nil, err | ||
} | ||
log.Printf("Loaded %v", digestTag) | ||
|
||
for _, tagName := range k.tags { | ||
log.Printf("Adding tag %s", tagName) | ||
tag, err := name.NewTag(fmt.Sprintf("%s:%s", k.namer(K3sDomain, s), tagName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := k3s.Tag(ctx, digestTag, tag); err != nil { | ||
return nil, err | ||
} | ||
log.Printf("Added tag %v", tagName) | ||
} | ||
|
||
return &digestTag, nil | ||
} | ||
|
||
//Close implements publish.Interface | ||
func (k *k3sPublisher) Close() error { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2020 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package k3s defines methods for publishing images into k3s nodes. | ||
package k3s |
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,125 @@ | ||
package k3s | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/tarball" | ||
"golang.org/x/sync/errgroup" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
limaInstanceEnvKey = "LIMA_INSTANCE" | ||
rancherDesktopLimaInstanceKey = "0" | ||
) | ||
|
||
// Tag adds a tag to an already existent image. | ||
func Tag(ctx context.Context, src, dest name.Tag) error { | ||
li, ok := os.LookupEnv(limaInstanceEnvKey) | ||
if !ok { | ||
li = rancherDesktopLimaInstanceKey | ||
} | ||
env := buildCommandEnv(li) | ||
ctl, err := findNerdctl(li) | ||
if err != nil { | ||
return err | ||
} | ||
cmd := exec.CommandContext(ctx, ctl, "--namespace=k8s.io", "tag", src.String(), dest.String()) | ||
cmd.Env = env | ||
if err := cmd.Run(); err != nil { | ||
return fmt.Errorf("failed to tag image to instance %q: %w", li, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Write saves the image into the k3s nodes as the given tag. | ||
func Write(ctx context.Context, tag name.Tag, img v1.Image) error { | ||
pr, pw := io.Pipe() | ||
|
||
grp := errgroup.Group{} | ||
grp.Go(func() error { | ||
return pw.CloseWithError(tarball.Write(tag, img, pw)) | ||
}) | ||
|
||
li, ok := os.LookupEnv(limaInstanceEnvKey) | ||
//TODO(kamesh) for now its assumed that if no LIMA_INSTANCE env is defined it defaults to Rancher Desktop | ||
// is this safe assumption or need to find other ways?? | ||
if !ok { | ||
li = rancherDesktopLimaInstanceKey | ||
} | ||
env := buildCommandEnv(li) | ||
|
||
var stdErr bytes.Buffer | ||
//check of nerdctl exists on the system | ||
ctl, err := findNerdctl(li) | ||
if err != nil { | ||
return err | ||
} | ||
cmd := exec.CommandContext(ctx, ctl, "--namespace=k8s.io", "load") | ||
imjasonh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd.Stdin = pr | ||
cmd.Env = env | ||
cmd.Stderr = &stdErr | ||
if err := cmd.Run(); err != nil { | ||
log.Printf("%s", stdErr.String()) | ||
return fmt.Errorf("failed to load image to instance %q: %w", li, err) | ||
} | ||
|
||
if err := grp.Wait(); err != nil { | ||
return fmt.Errorf("failed to write intermediate tarball representation: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//buildCommandEnv adds the required environment variables that will be passed to the | ||
// command context | ||
//TODO(kamesh) add other required environment variables | ||
imjasonh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func buildCommandEnv(li string) []string { | ||
var env = make([]string, 5) | ||
|
||
env[0] = fmt.Sprintf("HOME=%s", os.Getenv("HOME")) | ||
env[1] = fmt.Sprintf("LIMA_INSTANCE=%s", li) | ||
imjasonh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
env[2] = fmt.Sprintf("PATH=%s", os.Getenv("PATH")) | ||
|
||
return env | ||
} | ||
|
||
//findNerdctl helps to find the nerdctl to use | ||
//TODO(kamesh) improve the nerdctl find | ||
//TODO(kamesh) not very efficient | ||
func findNerdctl(li string) (string, error) { | ||
var nerdctlPath string | ||
// use rancher desktop nerdctl wrapper script | ||
if li == "0" { | ||
f, err := exec.LookPath("nerdctl") | ||
if err != nil { | ||
return "", err | ||
} | ||
nerdctlPath, err = filepath.Abs(f) | ||
if err != nil { | ||
return "", err | ||
} | ||
} else { | ||
//if rancher desktop is on the system there should be an alternate script nerdctl.lima | ||
f, err1 := exec.LookPath("nerdctl.lima") | ||
nerdctlPath, err1 = filepath.Abs(f) | ||
if err1 != nil { | ||
return "", err1 | ||
} | ||
} | ||
|
||
_, err := os.Stat(nerdctlPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return nerdctlPath, nil | ||
} |
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.
I see this is largely copied from code in
publish/kind.go
-- I think there's an opportunity to share this code instead of duplicating it, especially since I think there are improvements we could make to it later. Could you refactor this into some shared method?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.
yeah I thought so to refactor, no worries I could that now
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.
@imjasonh - fixed this one with latest commit
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.
I am exploring the ways to do it as installing Rancher Desktop or Lima requires the hypervisors on the runner and I am finding way to install/enable it. IMHO from my previous experiences I was not able to do it. 🤔