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 helm chart build to artifact export #2180

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions cmd/kk/cmd/artifact/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewCmdArtifactImages() *cobra.Command {

o.CommonOptions.AddCommonFlag(cmd)
cmd.AddCommand(NewCmdArtifactImagesPush())
cmd.AddCommand(NewCmdArtifactImagesList())

return cmd
}
112 changes: 112 additions & 0 deletions cmd/kk/cmd/artifact/images/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright 2022 The KubeSphere Authors.

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 images

import (
"fmt"
kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2"
"github.com/kubesphere/kubekey/v3/cmd/kk/cmd/options"
"github.com/kubesphere/kubekey/v3/cmd/kk/cmd/util"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/images"
"github.com/spf13/cobra"
)

type ArtifactImagesListOptions struct {
CommonOptions *options.CommonOptions

KubernetesVersion string
}

func NewArtifactImagesListOptions() *ArtifactImagesListOptions {
return &ArtifactImagesListOptions{
CommonOptions: options.NewCommonOptions(),
}
}

// NewCmdArtifactImagesList creates a new `kubekey artifacts images push` command
func NewCmdArtifactImagesList() *cobra.Command {
o := NewArtifactImagesListOptions()
cmd := &cobra.Command{
Use: "list",
Short: "list images to a registry from an artifact",
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Run())
},
}

o.CommonOptions.AddCommonFlag(cmd)
o.AddFlags(cmd)
return cmd
}

func (o *ArtifactImagesListOptions) Run() error {

imageName := []string{
"pause",
"etcd",
"kube-apiserver",
"kube-controller-manager",
"kube-scheduler",
"kube-proxy",

// network
"coredns",
"k8s-dns-node-cache",
"calico-kube-controllers",
"calico-cni",
"calico-node",
"calico-flexvol",
"calico-typha",
"flannel",
"flannel-cni-plugin",
"cilium",
"cilium-operator-generic",
"hybridnet",
"kubeovn",
"multus",
// storage
"provisioner-localpv",
"linux-utils",
// load balancer
"haproxy",
"kubevip",
// kata-deploy
"kata-deploy",
// node-feature-discovery
"node-feature-discovery",
}

for _, name := range imageName {
repo := images.GetImage(&common.KubeRuntime{
BaseRuntime: connector.NewBaseRuntime("image list", connector.NewDialer(), o.CommonOptions.Verbose, o.CommonOptions.IgnoreErr),
}, &common.KubeConf{
Cluster: &kubekeyapiv1alpha2.ClusterSpec{
Kubernetes: kubekeyapiv1alpha2.Kubernetes{Version: o.KubernetesVersion},
Registry: kubekeyapiv1alpha2.RegistryConfig{PrivateRegistry: "docker.io"},
},
}, name).ImageName()
fmt.Println(repo)
}

return nil
}

func (o *ArtifactImagesListOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.KubernetesVersion, "k8s-version", "", "v1.23.15", "Path to a KubeKey artifact images directory")
}
1 change: 0 additions & 1 deletion cmd/kk/pkg/addons/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func ToOptions(flags *apply.ApplyFlags, manifests []string, version string) (*ap
All: flags.All,
Overwrite: flags.Overwrite,
OpenAPIPatch: flags.OpenAPIPatch,
PruneWhitelist: flags.PruneWhitelist,

Recorder: recorder,
Namespace: namespace,
Expand Down
96 changes: 94 additions & 2 deletions cmd/kk/pkg/images/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ package images

import (
"context"
"os"

"encoding/json"
"fmt"
"github.com/containerd/containerd/platforms"
"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/transports/alltransports"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"helm.sh/helm/v3/pkg/registry"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/errdef"
"os"
)

const (
MediaTypeConfig = "application/vnd.docker.container.image.v1+json"
MediaTypeManifestList = "application/vnd.docker.distribution.manifest.list.v2+json"
MediaTypeManifest = "application/vnd.docker.distribution.manifest.v2+json"
MediaTypeForeignLayer = "application/vnd.docker.image.rootfs.foreign.diff.tar.gzip"
)

type CopyImageOptions struct {
Expand Down Expand Up @@ -62,6 +76,33 @@ func (c *CopyImageOptions) Copy() error {
return nil
}

func (c *CopyImageOptions) OrasCopy() error {
srcTagger, err := c.srcImage.NewSrcTagger()
if err != nil {
return err
}
destTagger, err := c.destImage.NewDestTagger()
if err != nil {
return err
}
options := oras.DefaultCopyOptions

options.MapRoot = func(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor) (ocispec.Descriptor, error) {
return mapRoot(ctx, src, root, ocispec.Platform{
Architecture: c.srcImage.dockerImage.arch,
OS: c.srcImage.dockerImage.os,
Variant: c.srcImage.dockerImage.variant,
})
}

_, err = oras.Copy(context.TODO(), srcTagger, c.srcImage.imageName, destTagger, c.destImage.imageName, options)
if err != nil {
return err
}

return nil
}

func getPolicyContext() (*signature.PolicyContext, error) {
policy := &signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}}
return signature.NewPolicyContext(policy)
Expand All @@ -73,6 +114,7 @@ type Index struct {

type Manifest struct {
Annotations annotations
Platform ocispec.Platform `json:"platform,omitempty"`
}

type annotations struct {
Expand All @@ -84,3 +126,53 @@ func NewIndex() *Index {
Manifests: []Manifest{},
}
}

func mapRoot(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor, p ocispec.Platform) (ocispec.Descriptor, error) {

all, err := content.FetchAll(ctx, src, root)
if err != nil {
return ocispec.Descriptor{}, err
}

switch root.MediaType {
case MediaTypeManifestList, ocispec.MediaTypeImageIndex:
var index ocispec.Index
if err := json.Unmarshal(all, &index); err != nil {
return ocispec.Descriptor{}, err
}
for _, manifest := range index.Manifests {
matcher := platforms.NewMatcher(*manifest.Platform)
if matcher.Match(p) {
return manifest, nil
}
}
return ocispec.Descriptor{}, fmt.Errorf("%s: %w: no matching manifest was found in the manifest list", root.Digest, errdef.ErrNotFound)
default:
var configDesc ocispec.Manifest
err = json.Unmarshal(all, &configDesc)
if err != nil {
return ocispec.Descriptor{}, err
}

if configDesc.Config.MediaType == registry.ConfigMediaType {
return root, nil
}

fetch, err := src.Fetch(ctx, configDesc.Config)
if err != nil {
return ocispec.Descriptor{}, err
}
var configPlat ocispec.Platform
err = json.NewDecoder(fetch).Decode(&configPlat)
if err != nil {
return ocispec.Descriptor{}, err
}
matcher := platforms.NewMatcher(configPlat)
if matcher.Match(p) {
return root, nil
}

return ocispec.Descriptor{}, fmt.Errorf("fail to recognize platform from unknown config %s", root.MediaType)
}

}
2 changes: 0 additions & 2 deletions cmd/kk/pkg/images/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ func (c *CopyImagesToRegistryModule) Init() {
Desc: "Copy images to a private registry from an artifact OCI Path",
Action: &CopyImagesToRegistry{ImagesPath: c.ImagePath},
}

pushManifest := &task.LocalTask{
Name: "PushManifest",
Desc: "Push multi-arch manifest to private registry",
Action: new(PushManifest),
}

c.Tasks = []task.Interface{
copyImage,
pushManifest,
Expand Down
Loading
Loading