一、操作步骤

1、思路

位置:

lifted文件的位置在github.com\karmada-io\karmada\pkg\util\lifted

背景:

为避免vendor文件过大,和karmada的被动依赖组件过多。将编程中使用的依赖软件的函数或变量拷贝出来,放入lifted目录下,直接引用。

拷贝出代码需要保持之前的开源协议。并注明路径,版本甚至引用行号。

由于不能独立于go的依赖管理方式,故需要定期与开源软件同步:减少代码差异,相关修复bug/漏洞/设计方式改变;

同步思路:

获取lifted下文件的记录链接=》获取新的链接地址(修改版本号)=》下载对应文件=》使用工具对比差异=》同步代码=》测试test=》静态检查=》何入主干。

2、步骤

1)、编写同步代码:https://github.com/zishen/kuberiver

2)、执行得到新的文件。

3)、使用WindMerge对比文件。重点放在函数实现变化。

若函数名称变化,需要找到新函数和对应的pr搞清楚原因。通过在对应代码库中全局查找函数名称。记录如下:

变化函数:
NodeSelectorRequirementsAsSelector
旧链接:
https://github.com/kubernetes/kubernetes/blob/release-1.23/pkg/apis/core/helper/helpers.go#L365-L397
新链接:
https://github.com/kubernetes/kubernetes/blob/release-1.27/staging/src/k8s.io/component-helpers/scheduling/corev1/nodeaffinity/nodeaffinity.go#LL203C1-L247C2
修改对应pr:
https://github.com/kubernetes/kubernetes/pull/96064

4)、使用make test在Linux服务器上进行本地测试。

5)、使用make update在Linux服务器上进行静态检查。

6)、提交pr。

二、问题记录

1、直接升级到1.27不行

+ go build -ldflags '-X github.com/karmada-io/karmada/pkg/version.gitVersion=v1.5.0-210-geffd5f8f-dirty -X github.com/karmada-io/karmada/pkg/version.gitCommit=effd5f8f81d1d2afce4b7f425f5120a0c956444e -X github.com/karmada-io/karmada/pkg/version.gitTreeState=dirty -X github.com/karmada-io/karmada/pkg/version.buildDate=2023-05-27T06:33:39Z ' -o _output/bin/linux/amd64/karmada-controller-manager github.com/karmada-io/karmada/cmd/controller-manager
# github.com/karmada-io/karmada/pkg/util/restmapper
pkg/util/restmapper/restmapper.go:97:20: undefined: apiutil.WithCustomMapper
# sigs.k8s.io/cluster-api/api/v1beta1
vendor/sigs.k8s.io/cluster-api/api/v1beta1/machine_webhook.go:45:27: cannot use &Machine{} (value of type *Machine) as admission.Validator value in variable declaration: *Machine does not implement admission.Validator (wrong type for method ValidateCreate)
		have ValidateCreate() error
		want ValidateCreate() (admission.Warnings, error)
vendor/sigs.k8s.io/cluster-api/api/v1beta1/machinedeployment_webhook.go:56:27: cannot use &MachineDeployment{} (value of type *MachineDeployment) as admission.Validator value in variable declaration: *MachineDeployment does not implement admission.Validator (wrong type for method ValidateCreate)
		have ValidateCreate() error
		want ValidateCreate() (admission.Warnings, error)
vendor/sigs.k8s.io/cluster-api/api/v1beta1/machinedeployment_webhook.go:61:16: assignment mismatch: 2 variables but admission.NewDecoder returns 1 value
vendor/sigs.k8s.io/cluster-api/api/v1beta1/machinehealthcheck_webhook.go:63:27: cannot use &MachineHealthCheck{} (value of type *MachineHealthCheck) as admission.Validator value in variable declaration: *MachineHealthCheck does not implement admission.Validator (wrong type for method ValidateCreate)
		have ValidateCreate() error
		want ValidateCreate() (admission.Warnings, error)
vendor/sigs.k8s.io/cluster-api/api/v1beta1/machineset_webhook.go:45:27: cannot use &MachineSet{} (value of type *MachineSet) as admission.Validator value in variable declaration: *MachineSet does not implement admission.Validator (wrong type for method ValidateCreate)
		have ValidateCreate() error
		want ValidateCreate() (admission.Warnings, error)
Makefile:42: recipe for target 'karmada-controller-manager' failed
make[1]: *** [karmada-controller-manager] Error 1
make[1]: Leaving directory '/usr/workspace/go/src/karmada-io/karmada'
Makefile:57: recipe for target 'image-karmada-controller-manager' failed
make: *** [image-karmada-controller-manager] Error 2
make: Leaving directory '/usr/workspace/go/src/karmada-io/karmada'

go.mod修改如下时:

module github.com/karmada-io/karmada

go 1.20

require (
	github.com/distribution/distribution/v3 v3.0.0-20210507173845-9329f6a62b67
	github.com/emirpasic/gods v1.18.1
	github.com/evanphx/json-patch/v5 v5.6.0
	github.com/gogo/protobuf v1.3.2
	github.com/golang/mock v1.6.0
	github.com/google/go-cmp v0.5.9
	github.com/kr/pretty v0.3.1
	github.com/olekukonko/tablewriter v0.0.5
	github.com/onsi/ginkgo/v2 v2.9.5
	github.com/onsi/gomega v1.27.7
	github.com/opensearch-project/opensearch-go v1.1.0
	github.com/pkg/errors v0.9.1
	github.com/prometheus/client_golang v1.15.1
	github.com/spf13/cobra v1.6.1
	github.com/spf13/pflag v1.0.5
	github.com/stretchr/testify v1.8.1
	github.com/vektra/mockery/v2 v2.10.0
	github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64
	go.uber.org/atomic v1.9.0
	golang.org/x/term v0.8.0
	golang.org/x/text v0.9.0
	golang.org/x/time v0.3.0
	golang.org/x/tools v0.9.1
	gomodules.xyz/jsonpatch/v2 v2.3.0
	google.golang.org/grpc v1.52.0
	gopkg.in/yaml.v3 v3.0.1
	k8s.io/api v0.27.2
	k8s.io/apiextensions-apiserver v0.27.2
	k8s.io/apimachinery v0.27.2
	k8s.io/apiserver v0.27.2
	k8s.io/cli-runtime v0.27.2
	k8s.io/client-go v0.27.2
	k8s.io/cluster-bootstrap v0.27.2
	k8s.io/code-generator v0.27.2
	k8s.io/component-base v0.27.2
	k8s.io/component-helpers v0.27.2
	k8s.io/klog/v2 v2.90.1
	k8s.io/kube-aggregator v0.27.2
	k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f
	k8s.io/kubectl v0.27.2
	k8s.io/kubernetes v1.27.2
	k8s.io/utils v0.0.0-20230209194617-a36077c30491
	layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
	sigs.k8s.io/cluster-api v1.4.2
	sigs.k8s.io/controller-runtime v0.15.0
	sigs.k8s.io/kind v0.19.0
	sigs.k8s.io/mcs-api v0.1.0
	sigs.k8s.io/structured-merge-diff/v4 v4.2.3
	sigs.k8s.io/yaml v1.3.0
)

require (
	github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
	github.com/BurntSushi/toml v1.0.0 // indirect
	github.com/MakeNowJust/heredoc v1.0.0 // indirect
	github.com/NYTimes/gziphandler v1.1.1 // indirect
	github.com/alessio/shellescape v1.4.1 // indirect
	github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
	github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
	github.com/beorn7/perks v1.0.1 // indirect
	github.com/blang/semver v3.5.1+incompatible // indirect
	github.com/blang/semver/v4 v4.0.0 // indirect
	github.com/cenkalti/backoff/v4 v4.1.3 // indirect
	github.com/cespare/xxhash/v2 v2.2.0 // indirect
	github.com/chai2010/gettext-go v1.0.2 // indirect
	github.com/coreos/go-semver v0.3.0 // indirect
	github.com/coreos/go-systemd/v22 v22.4.0 // indirect
	github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/emicklei/go-restful/v3 v3.9.0 // indirect
	github.com/evanphx/json-patch v5.6.0+incompatible // indirect
	github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
	github.com/fatih/camelcase v1.0.0 // indirect
	github.com/felixge/httpsnoop v1.0.3 // indirect
	github.com/fsnotify/fsnotify v1.6.0 // indirect
	github.com/fvbommel/sortorder v1.0.1 // indirect
	github.com/go-errors/errors v1.4.2 // indirect
	github.com/go-logr/logr v1.2.4 // indirect
	github.com/go-logr/stdr v1.2.2 // indirect
	github.com/go-logr/zapr v1.2.4 // indirect
	github.com/go-openapi/jsonpointer v0.19.6 // indirect
	github.com/go-openapi/jsonreference v0.20.1 // indirect
	github.com/go-openapi/swag v0.22.3 // indirect
	github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
	github.com/gobuffalo/flect v1.0.2 // indirect
	github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
	github.com/golang/protobuf v1.5.3 // indirect
	github.com/google/btree v1.0.1 // indirect
	github.com/google/cel-go v0.12.6 // indirect
	github.com/google/gnostic v0.6.9 // indirect
	github.com/google/gofuzz v1.2.0 // indirect
	github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
	github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect
	github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
	github.com/google/uuid v1.3.0 // indirect
	github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
	github.com/hashicorp/hcl v1.0.0 // indirect
	github.com/imdario/mergo v0.3.13 // indirect
	github.com/inconshreveable/mousetrap v1.0.1 // indirect
	github.com/jonboulle/clockwork v0.2.2 // indirect
	github.com/josharian/intern v1.0.0 // indirect
	github.com/json-iterator/go v1.1.12 // indirect
	github.com/kr/text v0.2.0 // indirect
	github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
	github.com/magiconair/properties v1.8.7 // indirect
	github.com/mailru/easyjson v0.7.7 // indirect
	github.com/mattn/go-isatty v0.0.17 // indirect
	github.com/mattn/go-runewidth v0.0.14 // indirect
	github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
	github.com/mitchellh/go-homedir v1.1.0 // indirect
	github.com/mitchellh/go-wordwrap v1.0.0 // indirect
	github.com/mitchellh/mapstructure v1.5.0 // indirect
	github.com/moby/spdystream v0.2.0 // indirect
	github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
	github.com/modern-go/reflect2 v1.0.2 // indirect
	github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
	github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
	github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
	github.com/opencontainers/go-digest v1.0.0 // indirect
	github.com/pelletier/go-toml v1.9.4 // indirect
	github.com/pelletier/go-toml/v2 v2.0.6 // indirect
	github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	github.com/prometheus/client_model v0.4.0 // indirect
	github.com/prometheus/common v0.42.0 // indirect
	github.com/prometheus/procfs v0.9.0 // indirect
	github.com/rivo/uniseg v0.4.2 // indirect
	github.com/rogpeppe/go-internal v1.10.0 // indirect
	github.com/rs/zerolog v1.26.1 // indirect
	github.com/russross/blackfriday/v2 v2.1.0 // indirect
	github.com/spf13/afero v1.9.3 // indirect
	github.com/spf13/cast v1.5.0 // indirect
	github.com/spf13/jwalterweatherman v1.1.0 // indirect
	github.com/spf13/viper v1.15.0 // indirect
	github.com/stoewer/go-strcase v1.2.0 // indirect
	github.com/stretchr/objx v0.5.0 // indirect
	github.com/subosito/gotenv v1.4.2 // indirect
	github.com/xlab/treeprint v1.1.0 // indirect
	go.etcd.io/etcd/api/v3 v3.5.7 // indirect
	go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
	go.etcd.io/etcd/client/v3 v3.5.7 // indirect
	go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.35.0 // indirect
	go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.35.1 // indirect
	go.opentelemetry.io/otel v1.10.0 // indirect
	go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 // indirect
	go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0 // indirect
	go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0 // indirect
	go.opentelemetry.io/otel/metric v0.31.0 // indirect
	go.opentelemetry.io/otel/sdk v1.10.0 // indirect
	go.opentelemetry.io/otel/trace v1.10.0 // indirect
	go.opentelemetry.io/proto/otlp v0.19.0 // indirect
	go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
	go.uber.org/multierr v1.8.0 // indirect
	go.uber.org/zap v1.24.0 // indirect
	golang.org/x/crypto v0.3.0 // indirect
	golang.org/x/mod v0.10.0 // indirect
	golang.org/x/net v0.10.0 // indirect
	golang.org/x/oauth2 v0.6.0 // indirect
	golang.org/x/sync v0.2.0 // indirect
	golang.org/x/sys v0.8.0 // indirect
	google.golang.org/appengine v1.6.7 // indirect
	google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
	google.golang.org/protobuf v1.30.0 // indirect
	gopkg.in/inf.v0 v0.9.1 // indirect
	gopkg.in/ini.v1 v1.67.0 // indirect
	gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
	gopkg.in/square/go-jose.v2 v2.6.0 // indirect
	gopkg.in/yaml.v2 v2.4.0 // indirect
	k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
	k8s.io/kms v0.27.2 // indirect
	sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.1.2 // indirect
	sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
	sigs.k8s.io/kustomize/api v0.13.2 // indirect
	sigs.k8s.io/kustomize/kyaml v0.14.1 // indirect
)

2、升级到1.26,引入改为lifted

会有如下引入存在问题:

	k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
	k8s.io/utils v0.0.0-20221128185143-99ec85e7a448

会变为:

	k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f
	k8s.io/utils v0.0.0-20230209194617-a36077c30491

如下模块导致:

go: finding module for package k8s.io/kubernetes/pkg/apis/core
go: finding module for package k8s.io/kubernetes/pkg/scheduler/metrics
go: finding module for package k8s.io/kubernetes/pkg/api/v1/pod //升级1.27引入
go: found k8s.io/kubernetes/pkg/apis/core in k8s.io/kubernetes v1.27.2
go: found k8s.io/kubernetes/pkg/api/v1/pod in k8s.io/kubernetes v1.27.2
go: found k8s.io/kubernetes/pkg/scheduler/metrics in k8s.io/kubernetes v1.27.2

故需要修改对应的lifted代码。参考之前的代码,不做变化。

3、test用例问题

requestinfo.go中这个部分不能和源代码保持,需要和之前版本一致(不支持fieldSelector):不能加回来。

func NewRequestInfo(req *http.Request) *apirequest.RequestInfo {
    ...
    		if opts.FieldSelector != nil {
			if name, ok := opts.FieldSelector.RequiresExactMatch("metadata.name"); ok {
				if len(path.IsValidPathSegmentName(name)) == 0 {
					requestInfo.Name = name
				}
			}
		}
    ...
}

4、go package的别名问题

需要遵循hack/.import-aliases中定义的规则。

如:

import (
	"sort"

	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
)

不能将appsv1省略或改为v1。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐