Skip to content
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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
return publish.NewKindPublisher(namer, po.Tags), nil
}

// handle the k3s distros
if repoName == publish.K3sDomain {
return publish.NewK3sPublisher(namer, po.Tags), nil
}

if repoName == "" && po.Push {
return nil, errors.New("KO_DOCKER_REPO environment variable is unset")
}
Expand Down
114 changes: 114 additions & 0 deletions pkg/publish/k3s.go
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")
Copy link
Member

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?

Copy link
Author

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

Copy link
Author

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

Copy link
Author

@kameshsampath kameshsampath Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to see some kind of e2e test, sort of like we have in kind-e2e.yaml -- to ensure this works, and that future changes don't break it. Is that something we could add relatively easily?

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. 🤔

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
}
16 changes: 16 additions & 0 deletions pkg/publish/k3s/doc.go
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
125 changes: 125 additions & 0 deletions pkg/publish/k3s/write.go
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")
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
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)
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
}