Skip to content

Commit

Permalink
add oci registry
Browse files Browse the repository at this point in the history
Signed-off-by: jsparter <[email protected]>
  • Loading branch information
jsparter committed Apr 14, 2023
1 parent d103c69 commit d24965b
Show file tree
Hide file tree
Showing 121 changed files with 1,179 additions and 8,973 deletions.
10 changes: 5 additions & 5 deletions build/buildimage/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ var parseContainerImageListFuncMap = map[string]func(srcPath string) ([]*v12.Con

type Registry struct {
platform v1.Platform
puller save.ImageSave
puller save.ImageSaver
}

func NewRegistry(platform v1.Platform) *Registry {
func NewRegistry(platform v1.Platform, registryType string) *Registry {
ctx := context.Background()
return &Registry{
platform: platform,
puller: save.NewImageSaver(ctx),
puller: save.NewImageSaver(ctx, registryType),
}
}

Expand Down Expand Up @@ -164,11 +164,11 @@ func ParseContainerImageSlice(srcPath string) ([]string, error) {
// NewRegistryDiffer
// Deprecated
// TODO: delete RegistryDiffer
func NewRegistryDiffer(platform v1.Platform) Differ {
func NewRegistryDiffer(platform v1.Platform, registryType string) Differ {
ctx := context.Background()
return &Registry{
platform: platform,
puller: save.NewImageSaver(ctx),
puller: save.NewImageSaver(ctx, registryType),
}
}

Expand Down
8 changes: 5 additions & 3 deletions build/buildimage/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package buildimage

import (
"context"
"fmt"
"path/filepath"

Expand All @@ -33,14 +34,15 @@ type ImageSection struct {
}

type MiddlewarePuller struct {
puller save.DefaultImageSaver
puller save.ImageSaver
platform v1.Platform
}

func NewMiddlewarePuller(platform v1.Platform) MiddlewarePuller {
func NewMiddlewarePuller(platform v1.Platform, registryType string) MiddlewarePuller {
puller := save.NewImageSaver(context.Background(), registryType)
return MiddlewarePuller{
platform: platform,
puller: save.DefaultImageSaver{},
puller: puller,
}
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/sealer/cmd/cluster/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"path/filepath"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"
"github.com/sealerio/sealer/cmd/sealer/cmd/utils"
"github.com/sealerio/sealer/common"
Expand All @@ -30,8 +33,6 @@ import (
"github.com/sealerio/sealer/pkg/infradriver"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils/strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var applyFlags *types.ApplyFlags
Expand Down
26 changes: 22 additions & 4 deletions cmd/sealer/cmd/cluster/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"
"github.com/sealerio/sealer/cmd/sealer/cmd/utils"
"github.com/sealerio/sealer/common"
Expand All @@ -34,9 +38,6 @@ import (
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils/platform"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)

var runFlags *types.RunFlags
Expand Down Expand Up @@ -100,11 +101,13 @@ func NewRunCmd() *cobra.Command {
Image: args[0],
Platform: "local",
})

if err != nil {
return err
}

imageSpec, err := imageEngine.Inspect(&options.InspectOptions{ImageNameOrID: id})

if err != nil {
return fmt.Errorf("failed to get cluster image extension: %s", err)
}
Expand Down Expand Up @@ -205,7 +208,7 @@ func runWithClusterfile(clusterFile string, runFlags *types.RunFlags) error {

imageSpec, err := imageEngine.Inspect(&options.InspectOptions{ImageNameOrID: id})
if err != nil {
return fmt.Errorf("failed to get cluster image extension: %s", err)
return fmt.Errorf("failed to get cluster image spec: %s", err)
}

if imageSpec.ImageExtension.Type == imagev1.AppInstaller {
Expand Down Expand Up @@ -371,6 +374,21 @@ func installApplication(appImageName string, envs []string, app *v2.Application,
return err
}

imageSpec, err := imageEngine.Inspect(&options.InspectOptions{ImageNameOrID: cluster.Spec.Image})
if err != nil {
return fmt.Errorf("failed to get cluster image extension: %s", err)
}
clusterRegistryType := imageSpec.ImageExtension.GetRegistryType()
applicationRegistryType := extension.GetRegistryType()
if clusterRegistryType != applicationRegistryType {
if clusterRegistryType == common.OCIRegistryType &&
applicationRegistryType == common.DefaultRegistryType {
return fmt.Errorf("current cluster using OCI registry, can't run app-installer with docker registry over cluster with OCI registry")
}
// TODO: need a tool to copy OCI images to docker registry
return fmt.Errorf("current cluster using docker registry, can't run app-installer with OCI registry over cluster with docker registry now. Remained to be implemented")
}

infraDriver.AddClusterEnv(envs)

clusterHosts := infraDriver.GetHostIPList()
Expand Down
5 changes: 2 additions & 3 deletions cmd/sealer/cmd/cluster/scale-up.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/sealerio/sealer/common"
clusterruntime "github.com/sealerio/sealer/pkg/cluster-runtime"
"github.com/sealerio/sealer/pkg/clusterfile"
imagecommon "github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imagedistributor"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sealerio/sealer/pkg/infradriver"
Expand Down Expand Up @@ -82,7 +82,7 @@ func NewScaleUpCmd() *cobra.Command {
return err
}

imageEngine, err := imageengine.NewImageEngine(imagecommon.EngineGlobalConfigurations{})
imageEngine, err := imageengine.NewImageEngine(options.EngineGlobalConfigurations{})
if err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +159,6 @@ func scaleUpCluster(clusterImageName string, scaleUpMasterIPList, scaleUpNodeIPL
if err != nil {
return err
}

//we need to save desired clusterfile to local disk temporarily.
//and will use it later to clean the cluster node if ScaleUp failed.
if err = cf.SaveAll(clusterfile.SaveOptions{}); err != nil {
Expand Down
12 changes: 9 additions & 3 deletions cmd/sealer/cmd/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/sealerio/sealer/build/buildimage"
"github.com/sealerio/sealer/build/kubefile/parser"
"github.com/sealerio/sealer/common"
version2 "github.com/sealerio/sealer/pkg/define/application/version"
v12 "github.com/sealerio/sealer/pkg/define/image/v1"
"github.com/sealerio/sealer/pkg/define/options"
Expand Down Expand Up @@ -112,6 +113,7 @@ func NewBuildCmd() *cobra.Command {
buildCmd.Flags().StringVar(&buildFlags.IgnoredImageList, "ignored-image-list", "filepath", "`pathname` of ignored image list filepath, if set, sealer will read its contents and prevent downloading of the corresponding container image")
buildCmd.Flags().StringVar(&buildFlags.PullPolicy, "pull", "ifnewer", "pull policy. Allow for --pull, --pull=true, --pull=false, --pull=never, --pull=always, --pull=ifnewer")
buildCmd.Flags().StringVar(&buildFlags.ImageType, "type", v12.KubeInstaller, fmt.Sprintf("specify the image type, --type=%s, --type=%s, default is %s", v12.KubeInstaller, v12.AppInstaller, v12.KubeInstaller))
buildCmd.Flags().StringVar(&buildFlags.RegistryType, "registry-type", common.DefaultRegistryType, fmt.Sprintf("set internal registry type, --registry-type=%s, --registry-type=%s, default is %s", common.DefaultRegistryType, common.OCIRegistryType, common.DefaultRegistryType))
buildCmd.Flags().StringSliceVar(&buildFlags.Platforms, "platform", []string{parse.DefaultPlatform()}, "set the target platform, --platform=linux/amd64 or --platform=linux/amd64/v7. Multi-platform will be like --platform=linux/amd64,linux/amd64/v7")
buildCmd.Flags().StringSliceVar(&buildFlags.BuildArgs, "build-arg", []string{}, "set custom build args")
buildCmd.Flags().StringSliceVar(&buildFlags.Annotations, "annotation", []string{}, "add annotations for image. Format like --annotation key=[value]")
Expand All @@ -122,7 +124,10 @@ func NewBuildCmd() *cobra.Command {
if _, ok := supportedImageType[buildFlags.ImageType]; !ok {
logrus.Fatalf("image type %s is not supported", buildFlags.ImageType)
}

supportedRegistryType := map[string]struct{}{common.DefaultRegistryType: {}, common.OCIRegistryType: {}}
if _, ok := supportedRegistryType[buildFlags.RegistryType]; !ok {
logrus.Fatalf("registry type %s is not supported", buildFlags.RegistryType)
}
return buildCmd
}

Expand Down Expand Up @@ -316,7 +321,7 @@ func applyRegistryToImage(engine imageengine.Interface, imageID string, platform
Architecture: arch,
OS: _os,
Variant: variant,
})
}, buildFlags.RegistryType)
if err := registry.SaveImages(tmpDirForLink, v12.GetImageSliceFromContainerImageList(containerImageList)); err != nil {
return "", nil, errors.Wrap(err, "failed to download container images")
}
Expand All @@ -343,7 +348,7 @@ func applyRegistryToImage(engine imageengine.Interface, imageID string, platform
Architecture: arch,
OS: _os,
Variant: variant,
}).PullWithImageSection(tmpDirForLink, imageSectionList); err != nil {
}, buildFlags.RegistryType).PullWithImageSection(tmpDirForLink, imageSectionList); err != nil {
return "", nil, err
}
}
Expand Down Expand Up @@ -433,6 +438,7 @@ func buildImageExtensionOnResult(result *parser.KubefileResult, imageType string
BuildClient: v12.BuildClient{
SealerVersion: version.Get().GitVersion,
BuildahVersion: define.Version,
RegistryType: buildFlags.RegistryType,
},
}

Expand Down
9 changes: 9 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,19 @@ const (
WINDOWS = "windows"
)

const (
DefaultRegistryType = "docker"
OCIRegistryType = "oci"
)

func GetSealerWorkDir() string {
return filepath.Join(GetHomeDir(), ".sealer")
}

func GetSealerConfigFile() string {
return filepath.Join(GetSealerWorkDir(), "config")
}

func GetDefaultClusterfile() string {
return filepath.Join(GetSealerWorkDir(), "Clusterfile")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster-runtime/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (i *Installer) Install() error {

var deployHosts []net.IP
if i.regConfig.LocalRegistry != nil {
installer := registry.NewInstaller(nil, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor)
installer := registry.NewInstaller(nil, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor, i.RuntimeConfig.ImageSpec.ImageExtension.GetRegistryType())
if *i.regConfig.LocalRegistry.HA {
deployHosts, err = installer.Reconcile(masters)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cluster-runtime/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (i *Installer) ScaleUp(newMasters, newWorkers []net.IP) (registry.Driver, r
if err != nil {
return nil, nil, err
}

// reconcile registry node if local registry is ha mode.
if i.regConfig.LocalRegistry != nil && *i.regConfig.LocalRegistry.HA {
registryDeployHosts, err = registry.NewInstaller(netutils.RemoveIPs(masters, newMasters), i.regConfig.LocalRegistry, i.infraDriver, i.Distributor).Reconcile(masters)
// i.ImageSpec.ImageExtension.GetRegistryType()
registryDeployHosts, err = registry.NewInstaller(netutils.RemoveIPs(masters, newMasters), i.regConfig.LocalRegistry, i.infraDriver, i.Distributor, i.ImageSpec.ImageExtension.GetRegistryType()).Reconcile(masters)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (i *Installer) ScaleDown(mastersToDelete, workersToDelete []net.IP) (regist

// reconcile registry node if local registry is ha mode.
if i.regConfig.LocalRegistry != nil && *i.regConfig.LocalRegistry.HA {
registryDeployHosts, err = registry.NewInstaller(masters, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor).Reconcile(netutils.RemoveIPs(masters, mastersToDelete))
registryDeployHosts, err = registry.NewInstaller(masters, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor, i.ImageSpec.ImageExtension.GetRegistryType()).Reconcile(netutils.RemoveIPs(masters, mastersToDelete))
if err != nil {
return nil, nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/cluster-runtime/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ func (i *Installer) UnInstall() error {

if i.regConfig.LocalRegistry != nil {
if *i.regConfig.LocalRegistry.HA {
installer := registry.NewInstaller(masters, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor)
installer := registry.NewInstaller(masters, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor, "")
err = installer.Clean()
if err != nil {
return err
}
}

installer := registry.NewInstaller([]net.IP{master0}, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor)
installer := registry.NewInstaller([]net.IP{master0}, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor, "")
err = installer.Clean()
if err != nil {
return err
Expand Down
56 changes: 56 additions & 0 deletions pkg/define/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright © 2022 Alibaba Group Holding Ltd.
//
// 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 define

import (
"encoding/json"
"os"
"path/filepath"

"github.com/sirupsen/logrus"

"github.com/sealerio/sealer/common"
osi "github.com/sealerio/sealer/utils/os"
)

type SealerConfig struct {
RegistryType string `json:"registryType,omitempty"` // docker、oci, default docker, "" is considered to be docker
}

// GetRegistryType read .sealer/config file and get registryType
func GetRegistryType() string {
sealerConfigFile := common.GetSealerConfigFile()
sealerConfig := &SealerConfig{}
if !osi.IsFileExist(sealerConfigFile) {
logrus.Warn("No .sealer/config exist, registryType consider to be docker")
return common.DefaultRegistryType
}
content, err := os.ReadFile(filepath.Clean(sealerConfigFile))
if err != nil {
logrus.Errorf("Read .sealer/config failed:%v, registryType consider to be docker", err)
return common.DefaultRegistryType
}
err = json.Unmarshal(content, sealerConfig)
if err != nil {
logrus.Errorf("unmarshal .sealer/config failed:%v, registryType consider to be docker", err)
return common.DefaultRegistryType
}
if sealerConfig.RegistryType != common.OCIRegistryType {
logrus.Errorf("unsupport registryType:%v, registryType consider to be docker", sealerConfig.RegistryType)
return common.DefaultRegistryType
}
logrus.Printf("registry type: %v", sealerConfig.RegistryType)
return sealerConfig.RegistryType
}
14 changes: 12 additions & 2 deletions pkg/define/image/v1/sealer_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package v1
import (
"encoding/json"

ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sealerio/sealer/common"
application_v1 "github.com/sealerio/sealer/pkg/define/application/v1"
"github.com/sealerio/sealer/pkg/define/application/version"
apiv1 "github.com/sealerio/sealer/types/api/v1"

ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

const (
Expand Down Expand Up @@ -107,6 +107,8 @@ type BuildClient struct {
SealerVersion string `json:"sealerVersion"`

BuildahVersion string `json:"buildahVersion"`

RegistryType string `json:"registryType,omitempty"` // docker、oci, default docker, "" is considered to be docker
}

type Launch struct {
Expand Down Expand Up @@ -163,3 +165,11 @@ func (ie *ImageExtension) UnmarshalJSON(data []byte) error {
(*ie).Launch = v1Ex.Launch
return nil
}

// GetRegistryType get registry type from imageExtension
func (ie *ImageExtension) GetRegistryType() string {
if ie.BuildClient.RegistryType == "" {
return common.DefaultRegistryType
}
return ie.BuildClient.RegistryType
}
1 change: 1 addition & 0 deletions pkg/define/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type BuildOptions struct {
ImageList string
ImageListWithAuth string
IgnoredImageList string
RegistryType string
}

type FromOptions struct {
Expand Down
Loading

0 comments on commit d24965b

Please sign in to comment.