Skip to content

Commit

Permalink
fix: karmada-operator install default version karmada
Browse files Browse the repository at this point in the history
Signed-off-by: chaosi-zju <chaosi@zju.edu.cn>
  • Loading branch information
chaosi-zju committed Oct 16, 2023
1 parent f56af66 commit 72900c6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 42 deletions.
4 changes: 4 additions & 0 deletions operator/cmd/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/karmada-io/karmada/operator/pkg/scheme"
"github.com/karmada-io/karmada/pkg/sharedcli"
"github.com/karmada-io/karmada/pkg/sharedcli/klogflag"
"github.com/karmada-io/karmada/pkg/version"
"github.com/karmada-io/karmada/pkg/version/sharedcommand"
)

// NewOperatorCommand creates a *cobra.Command object with default parameters
Expand Down Expand Up @@ -67,6 +69,7 @@ func NewOperatorCommand(ctx context.Context) *cobra.Command {
logsFlagSet := fss.FlagSet("logs")
klogflag.Add(logsFlagSet)

cmd.AddCommand(sharedcommand.NewCmdVersion("karmada-operator"))
cmd.Flags().AddFlagSet(genericFlagSet)
cmd.Flags().AddFlagSet(logsFlagSet)

Expand All @@ -77,6 +80,7 @@ func NewOperatorCommand(ctx context.Context) *cobra.Command {

// Run runs the karmada-operator. This should never exit.
func Run(ctx context.Context, o *options.Options) error {
klog.Infof("karmada-operator version: %s", version.Get())
klog.InfoS("Golang settings", "GOGC", os.Getenv("GOGC"), "GOMAXPROCS", os.Getenv("GOMAXPROCS"), "GOTRACEBACK", os.Getenv("GOTRACEBACK"))

manager, err := createControllerManager(ctx, o)
Expand Down
23 changes: 5 additions & 18 deletions operator/config/samples/karmada.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,14 @@ apiVersion: operator.karmada.io/v1alpha1
kind: Karmada
metadata:
name: karmada-demo
namespace: test
namespace: karmada-system
spec:
components:
etcd:
local:
imageRepository: registry.k8s.io/etcd
imageTag: 3.5.9-0
replicas: 1
volumeData:
# hostPath:
# type: DirectoryOrCreate
# path: /var/lib/karmada/etcd/karmada-demo
volumeClaim:
metadata:
name: etcd-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
karmadaAPIServer:
imageRepository: registry.k8s.io/kube-apiserver
imageTag: v1.25.4
Expand All @@ -31,19 +18,19 @@ spec:
serviceSubnet: 10.96.0.0/12
karmadaAggregatedAPIServer:
imageRepository: docker.io/karmada/karmada-aggregated-apiserver
imageTag: v1.6.0
imageTag: latest
replicas: 1
karmadaControllerManager:
imageRepository: docker.io/karmada/karmada-controller-manager
imageTag: v1.6.0
imageTag: latest
replicas: 1
karmadaScheduler:
imageRepository: docker.io/karmada/karmada-scheduler
imageTag: v1.6.0
imageTag: latest
replicas: 1
karmadaWebhook:
imageRepository: docker.io/karmada/karmada-webhook
imageTag: v1.6.0
imageTag: latest
replicas: 1
kubeControllerManager:
imageRepository: registry.k8s.io/kube-controller-manager
Expand Down
26 changes: 20 additions & 6 deletions operator/pkg/apis/operator/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"

"github.com/karmada-io/karmada/operator/pkg/constants"
"github.com/karmada-io/karmada/pkg/version"
)

var (
// DefaultKarmadaImageVersion defines the default of the karmada components image tag
DefaultKarmadaImageVersion string
etcdImageRepository = fmt.Sprintf("%s/%s", constants.KubeDefaultRepository, constants.Etcd)
karmadaAPIServiceImageRepository = fmt.Sprintf("%s/%s", constants.KubeDefaultRepository, constants.KubeAPIServer)
karmadaAggregatedAPIServerImageRepository = fmt.Sprintf("%s/%s", constants.KarmadaDefaultRepository, constants.KarmadaAggregatedAPIServer)
Expand All @@ -22,6 +26,16 @@ var (
KarmadaMetricsAdapterImageRepository = fmt.Sprintf("%s/%s", constants.KarmadaDefaultRepository, constants.KarmadaMetricsAdapter)
)

func init() {
releaseVer, err := version.ParseGitVersion(version.Get().GitVersion)
if err != nil {
klog.Infof("No default release version found. build version: %s", version.Get().String())
releaseVer = &version.ReleaseVersion{} // initialize to avoid panic
}
DefaultKarmadaImageVersion = releaseVer.ReleaseVersion()
klog.Infof("default Karmada Image Version: %s", DefaultKarmadaImageVersion)
}

func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
Expand Down Expand Up @@ -138,7 +152,7 @@ func setDefaultsKarmadaAggregatedAPIServer(obj *KarmadaComponents) {
aggregated.Image.ImageRepository = karmadaAggregatedAPIServerImageRepository
}
if len(aggregated.Image.ImageTag) == 0 {
aggregated.Image.ImageTag = constants.KarmadaDefaultVersion
aggregated.Image.ImageTag = DefaultKarmadaImageVersion
}
if aggregated.Replicas == nil {
aggregated.Replicas = pointer.Int32(1)
Expand Down Expand Up @@ -172,7 +186,7 @@ func setDefaultsKarmadaControllerManager(obj *KarmadaComponents) {
karmadaControllerManager.Image.ImageRepository = karmadaControllerManagerImageRepository
}
if len(karmadaControllerManager.Image.ImageTag) == 0 {
karmadaControllerManager.Image.ImageTag = constants.KarmadaDefaultVersion
karmadaControllerManager.Image.ImageTag = DefaultKarmadaImageVersion
}
if karmadaControllerManager.Replicas == nil {
karmadaControllerManager.Replicas = pointer.Int32(1)
Expand All @@ -189,7 +203,7 @@ func setDefaultsKarmadaScheduler(obj *KarmadaComponents) {
scheduler.Image.ImageRepository = karmadaSchedulerImageRepository
}
if len(scheduler.Image.ImageTag) == 0 {
scheduler.Image.ImageTag = constants.KarmadaDefaultVersion
scheduler.Image.ImageTag = DefaultKarmadaImageVersion
}
if scheduler.Replicas == nil {
scheduler.Replicas = pointer.Int32(1)
Expand All @@ -206,7 +220,7 @@ func setDefaultsKarmadaWebhook(obj *KarmadaComponents) {
webhook.Image.ImageRepository = karmadaWebhookImageRepository
}
if len(webhook.Image.ImageTag) == 0 {
webhook.Image.ImageTag = constants.KarmadaDefaultVersion
webhook.Image.ImageTag = DefaultKarmadaImageVersion
}
if webhook.Replicas == nil {
webhook.Replicas = pointer.Int32(1)
Expand All @@ -223,7 +237,7 @@ func setDefaultsKarmadaDescheduler(obj *KarmadaComponents) {
descheduler.Image.ImageRepository = karmadaDeschedulerImageRepository
}
if len(descheduler.Image.ImageTag) == 0 {
descheduler.Image.ImageTag = constants.KarmadaDefaultVersion
descheduler.Image.ImageTag = DefaultKarmadaImageVersion
}
if descheduler.Replicas == nil {
descheduler.Replicas = pointer.Int32(1)
Expand All @@ -240,7 +254,7 @@ func setDefaultsKarmadaMetricsAdapter(obj *KarmadaComponents) {
metricsAdapter.Image.ImageRepository = KarmadaMetricsAdapterImageRepository
}
if len(metricsAdapter.Image.ImageTag) == 0 {
metricsAdapter.Image.ImageTag = constants.KarmadaDefaultVersion
metricsAdapter.Image.ImageTag = DefaultKarmadaImageVersion
}

if metricsAdapter.Replicas == nil {
Expand Down
2 changes: 0 additions & 2 deletions operator/pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const (
KarmadaDefaultRepository = "docker.io/karmada"
// EtcdDefaultVersion defines the default of the karmada etcd image tag
EtcdDefaultVersion = "3.5.9-0"
// KarmadaDefaultVersion defines the default of the karmada components image tag
KarmadaDefaultVersion = "v1.7.0"
// KubeDefaultVersion defines the default of the karmada apiserver and kubeControllerManager image tag
KubeDefaultVersion = "v1.25.4"
// KarmadaDefaultServiceSubnet defines the default of the subnet used by k8s services.
Expand Down
4 changes: 2 additions & 2 deletions operator/pkg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ func defaultJobInitOptions() *InitOptions {
operatorscheme.Scheme.Default(karmada)

return &InitOptions{
CrdRemoteURL: fmt.Sprintf(defaultCrdURL, constants.KarmadaDefaultVersion),
KarmadaVersion: constants.KarmadaDefaultVersion,
CrdRemoteURL: fmt.Sprintf(defaultCrdURL, operatorv1alpha1.DefaultKarmadaImageVersion),
KarmadaVersion: operatorv1alpha1.DefaultKarmadaImageVersion,
KarmadaDataDir: constants.KarmadaDataDir,
Karmada: karmada,
}
Expand Down
14 changes: 11 additions & 3 deletions operator/pkg/tasks/init/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,18 @@ func runUnpack(r workflow.RunData) error {
func existCrdsTar(crdsDir string) bool {
files := util.ListFiles(crdsDir)

for _, file := range files {
if strings.Contains(file.Name(), crdsFileSuffix) && file.Size() > 0 {
return true
for _, filePath := range files {
if !strings.Contains(filePath, crdsFileSuffix) {
continue
}
file, err := os.Stat(filePath)
if err != nil {
continue
}
if file.Size() == 0 {
continue
}
return true
}
return false
}
15 changes: 7 additions & 8 deletions operator/pkg/tasks/init/karmadaresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func runCrds(r workflow.RunData) error {
}

func createCrds(crdsClient *crdsclient.Clientset, crdsPath string) error {
for _, file := range util.ListFiles(crdsPath) {
if file.IsDir() || path.Ext(file.Name()) != ".yaml" {
for _, filePath := range util.ListFiles(crdsPath) {
if path.Ext(filePath) != ".yaml" {
continue
}

crdBytes, err := util.ReadYamlFile(path.Join(crdsPath, file.Name()))
crdBytes, err := util.ReadYamlFile(filePath)
if err != nil {
return err
}
Expand All @@ -138,8 +138,8 @@ func createCrds(crdsClient *crdsclient.Clientset, crdsPath string) error {
}

func patchCrds(crdsClient *crdsclient.Clientset, patchPath string, caBundle string) error {
for _, file := range util.ListFiles(patchPath) {
if file.IsDir() || path.Ext(file.Name()) != ".yaml" {
for _, filePath := range util.ListFiles(patchPath) {
if path.Ext(filePath) != ".yaml" {
continue
}

Expand All @@ -148,13 +148,12 @@ func patchCrds(crdsClient *crdsclient.Clientset, patchPath string, caBundle stri
return err
}

crdPath := path.Join(patchPath, file.Name())
crdBytes, err := util.ReplaceYamlForReg(crdPath, caBundle, reg)
crdBytes, err := util.ReplaceYamlForReg(filePath, caBundle, reg)
if err != nil {
return err
}

crdResource := splitToCrdNameFormFile(file.Name(), "_", ".")
crdResource := splitToCrdNameFormFile(filePath, "_", ".")
name := crdResource + ".work.karmada.io"
if err := apiclient.PatchCustomResourceDefinition(crdsClient, name, crdBytes); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions operator/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ func ioCopyN(outFile *os.File, tr *tar.Reader) error {
}

// ListFiles traverse directory files
func ListFiles(path string) []os.FileInfo {
var files []os.FileInfo
func ListFiles(path string) []string {
var files []string
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, info)
files = append(files, path)
}
return nil
}); err != nil {
Expand Down

0 comments on commit 72900c6

Please sign in to comment.