Skip to content

Commit

Permalink
add static-pod-upgrade subcommand to node-servant
Browse files Browse the repository at this point in the history
Signed-off-by: hxcGit <[email protected]>
  • Loading branch information
xavier-hou committed Mar 29, 2023
1 parent faed9ea commit 7ce59ab
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 105 deletions.
2 changes: 2 additions & 0 deletions cmd/yurt-node-servant/node-servant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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) {
Expand Down
7 changes: 0 additions & 7 deletions hack/dockerfiles/build/Dockerfile.yurt-static-pod-upgrade

This file was deleted.

13 changes: 0 additions & 13 deletions hack/dockerfiles/release/Dockerfile.yurt-static-pod-upgrade

This file was deleted.

1 change: 0 additions & 1 deletion hack/make-rules/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions pkg/static-pod-upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
62 changes: 1 addition & 61 deletions pkg/static-pod-upgrade/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,16 @@ 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 (
YamlSuffix string = ".yaml"
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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 7ce59ab

Please sign in to comment.