diff --git a/cmd/yurt-node-servant/node-servant.go b/cmd/yurt-node-servant/node-servant.go index 8a5bd3210dc..001cb63ea59 100644 --- a/cmd/yurt-node-servant/node-servant.go +++ b/cmd/yurt-node-servant/node-servant.go @@ -28,6 +28,7 @@ import ( "github.com/openyurtio/openyurt/cmd/yurt-node-servant/convert" preflightconvert "github.com/openyurtio/openyurt/cmd/yurt-node-servant/preflight-convert" "github.com/openyurtio/openyurt/cmd/yurt-node-servant/revert" + upgrade "github.com/openyurtio/openyurt/cmd/yurt-node-servant/static-pod-upgrade" "github.com/openyurtio/openyurt/pkg/projectinfo" ) @@ -49,6 +50,7 @@ func main() { rootCmd.AddCommand(revert.NewRevertCmd()) rootCmd.AddCommand(preflightconvert.NewxPreflightConvertCmd()) rootCmd.AddCommand(config.NewConfigCmd()) + rootCmd.AddCommand(upgrade.NewUpgradeCmd()) if err := rootCmd.Execute(); err != nil { // run command os.Exit(1) diff --git a/cmd/yurt-static-pod-upgrade/static-pod-upgrade.go b/cmd/yurt-node-servant/static-pod-upgrade/upgrade.go similarity index 81% rename from cmd/yurt-static-pod-upgrade/static-pod-upgrade.go rename to cmd/yurt-node-servant/static-pod-upgrade/upgrade.go index 3d5ce28cd44..e3f00aa4725 100644 --- a/cmd/yurt-static-pod-upgrade/static-pod-upgrade.go +++ b/cmd/yurt-node-servant/static-pod-upgrade/upgrade.go @@ -14,19 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package upgrade import ( "fmt" - "math/rand" - "os" - "time" + "github.com/spf13/pflag" "github.com/spf13/cobra" - "github.com/spf13/pflag" "k8s.io/klog/v2" - "github.com/openyurtio/openyurt/pkg/projectinfo" upgrade "github.com/openyurtio/openyurt/pkg/static-pod-upgrade" ) @@ -35,20 +31,18 @@ var ( mode string ) -func main() { - rand.Seed(time.Now().UnixNano()) - version := fmt.Sprintf("%#v", projectinfo.Get()) +// NewUpgradeCmd generates a new upgrade command +func NewUpgradeCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "yurt-static-pod-upgrade", + Use: "static-pod-upgrade", + Short: "", Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("yurt-static-pod-upgrade version: %#v\n", version) - cmd.Flags().VisitAll(func(flag *pflag.Flag) { klog.Infof("FLAG: --%s=%q", flag.Name, flag.Value) }) if err := validate(); err != nil { - klog.Fatalf("Fail to validate yurt static pod upgrade args, %v", err) + klog.Fatalf("Fail to validate static pod upgrade args, %v", err) } ctrl, err := upgrade.New(manifest, mode) @@ -59,16 +53,14 @@ func main() { if err = ctrl.Upgrade(); err != nil { klog.Fatalf("Fail to upgrade static pod, %v", err) } + klog.Info("Static pod upgrade Success") }, - Version: version, + Args: cobra.NoArgs, } - addFlags(cmd) - if err := cmd.Execute(); err != nil { - os.Exit(1) - } + return cmd } func addFlags(cmd *cobra.Command) { diff --git a/hack/dockerfiles/build/Dockerfile.yurt-static-pod-upgrade b/hack/dockerfiles/build/Dockerfile.yurt-static-pod-upgrade deleted file mode 100644 index 1fc0108cae7..00000000000 --- a/hack/dockerfiles/build/Dockerfile.yurt-static-pod-upgrade +++ /dev/null @@ -1,7 +0,0 @@ -# multi-arch image building for yurt-static-pod-upgrade - -FROM --platform=${TARGETPLATFORM} alpine:3.17 -ARG TARGETOS TARGETARCH MIRROR_REPO -RUN if [ ! -z "${MIRROR_REPO+x}" ]; then sed -i "s/dl-cdn.alpinelinux.org/${MIRROR_REPO}/g" /etc/apk/repositories; fi && \ - apk add ca-certificates bash libc6-compat && update-ca-certificates && rm /var/cache/apk/* -COPY ./_output/local/bin/${TARGETOS}/${TARGETARCH}/yurt-static-pod-upgrade /usr/local/bin/yurt-static-pod-upgrade diff --git a/hack/dockerfiles/release/Dockerfile.yurt-static-pod-upgrade b/hack/dockerfiles/release/Dockerfile.yurt-static-pod-upgrade deleted file mode 100644 index 2c866cd2c72..00000000000 --- a/hack/dockerfiles/release/Dockerfile.yurt-static-pod-upgrade +++ /dev/null @@ -1,13 +0,0 @@ -# multi-arch image building for yurt-static-pod-upgrade - -FROM --platform=${BUILDPLATFORM} golang:1.18 as builder -ADD . /build -ARG TARGETOS TARGETARCH GIT_VERSION GOPROXY MIRROR_REPO -WORKDIR /build/ -RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GIT_VERSION=${GIT_VERSION} make build WHAT=cmd/yurt-static-pod-upgrade - -FROM --platform=${TARGETPLATFORM} alpine:3.17 -ARG TARGETOS TARGETARCH MIRROR_REPO -RUN if [ ! -z "${MIRROR_REPO+x}" ]; then sed -i "s/dl-cdn.alpinelinux.org/${MIRROR_REPO}/g" /etc/apk/repositories; fi && \ - apk add ca-certificates bash libc6-compat && update-ca-certificates && rm /var/cache/apk/* -COPY --from=builder /build/_output/local/bin/${TARGETOS}/${TARGETARCH}/yurt-static-pod-upgrade /usr/local/bin/yurt-static-pod-upgrade diff --git a/hack/make-rules/build.sh b/hack/make-rules/build.sh index 6b580c4c50c..1422dd5df7d 100755 --- a/hack/make-rules/build.sh +++ b/hack/make-rules/build.sh @@ -25,7 +25,6 @@ readonly YURT_ALL_TARGETS=( yurt-node-servant yurthub yurt-manager - yurt-static-pod-upgrade ) # clean old binaries at GOOS and GOARCH diff --git a/pkg/static-pod-upgrade/upgrade_test.go b/pkg/static-pod-upgrade/upgrade_test.go index 6c6022ab5b5..c0571536e39 100644 --- a/pkg/static-pod-upgrade/upgrade_test.go +++ b/pkg/static-pod-upgrade/upgrade_test.go @@ -59,11 +59,6 @@ func Test(t *testing.T) { /* 1. Prepare the test environment */ - if mode == "auto" { - runningStaticPod.Annotations = map[string]string{ - StaticPodHashAnnotation: TestHashValue, - } - } c := fake.NewSimpleClientset(runningStaticPod) // Add watch event for verify watcher := watch.NewFake() diff --git a/pkg/static-pod-upgrade/util.go b/pkg/static-pod-upgrade/util.go index 87a1aee8f00..1a65102d1b9 100644 --- a/pkg/static-pod-upgrade/util.go +++ b/pkg/static-pod-upgrade/util.go @@ -17,16 +17,8 @@ limitations under the License. package upgrade import ( - "context" - "fmt" "io" "os" - "time" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" - "k8s.io/klog/v2" ) const ( @@ -34,8 +26,7 @@ const ( BackupSuffix = ".bak" UpgradeSuffix string = ".upgrade" - StaticPodHashAnnotation = "openyurt.io/static-pod-hash" - OTALatestManifestAnnotation = "openyurt.io/ota-latest-version" + StaticPodHashAnnotation = "openyurt.io/static-pod-hash" ) func WithYamlSuffix(path string) string { @@ -70,54 +61,3 @@ func CopyFile(src, dst string) error { } return nil } - -// WaitForPodRunning waits static pod to run -// Success: Static pod annotation `StaticPodHashAnnotation` value equals to function argument hash -// Failed: Receive PodFailed event -func WaitForPodRunning(c kubernetes.Interface, name, namespace, hash string, timeout time.Duration) (bool, error) { - klog.Infof("WaitForPodRuning name is %s, namespace is %s", name, namespace) - // Create a watcher to watch the pod's status - watcher, err := c.CoreV1().Pods(namespace).Watch(context.TODO(), metav1.ListOptions{FieldSelector: "metadata.name=" + name}) - if err != nil { - return false, err - } - defer watcher.Stop() - - // Create a channel to receive updates from the watcher - ch := watcher.ResultChan() - - // Start a goroutine to monitor the pod's status - running := make(chan struct{}) - failed := make(chan struct{}) - - go func() { - for event := range ch { - obj, ok := event.Object.(*corev1.Pod) - if !ok { - continue - } - - h := obj.Annotations[StaticPodHashAnnotation] - - if obj.Status.Phase == corev1.PodRunning && h == hash { - close(running) - return - } - - if obj.Status.Phase == corev1.PodFailed { - close(failed) - return - } - } - }() - - // Wait for watch event to finish or the timeout to expire - select { - case <-running: - return true, nil - case <-failed: - return false, nil - case <-time.After(timeout): - return false, fmt.Errorf("timeout waiting for static pod %s/%s to be running", namespace, name) - } -}