Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function development -- yurtctl adds parameter enable app manager to control automatic deployment of yurtappmanager. #352

Merged
merged 3 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 122 additions & 7 deletions pkg/yurtctl/cmd/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ import (
"strings"
"time"

"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/openyurtio/openyurt/pkg/yurtctl/constants"
"github.com/openyurtio/openyurt/pkg/yurtctl/lock"
kubeutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/kubernetes"
strutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
"k8s.io/klog"
clusterinfophase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo"
nodeutil "k8s.io/kubernetes/pkg/controller/util/node"

"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/openyurtio/openyurt/pkg/yurtctl/constants"
"github.com/openyurtio/openyurt/pkg/yurtctl/lock"
kubeutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/kubernetes"
strutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/strings"
)

// Provider signifies the provider type
Expand Down Expand Up @@ -73,6 +74,9 @@ type ConvertOptions struct {
KubeadmConfPath string
DeployTunnel bool
kubeConfigPath string
EnableAppManager bool
YurtAppManagerImage string
yurtAppManagerClientSet dynamic.Interface
}

// NewConvertOptions creates a new ConvertOptions
Expand Down Expand Up @@ -132,6 +136,11 @@ func NewConvertCmd() *cobra.Command {
cmd.Flags().String("pod-manifest-path",
"/etc/kubernetes/manifests",
"Path to the directory on edge node containing static pod files.")
cmd.Flags().BoolP("enable-app-manager", "e", false,
"if set, yurtappmanager will be deployed.")
cmd.Flags().String("yurt-app-manager-image",
"openyurt/yurt-app-manager:v0.4.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think use tag latest is better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest version image can't be used without updating. Now we all use the version of v0.4.0.

"The yurt-app-manager image.")

return cmd
}
Expand All @@ -155,6 +164,12 @@ func (co *ConvertOptions) Complete(flags *pflag.FlagSet) error {
}
co.DeployTunnel = dt

eam, err := flags.GetBool("enable-app-manager")
if err != nil {
return err
}
co.EnableAppManager = eam

pStr, err := flags.GetString("provider")
if err != nil {
return err
Expand Down Expand Up @@ -197,6 +212,12 @@ func (co *ConvertOptions) Complete(flags *pflag.FlagSet) error {
}
co.YurttunnelAgentImage = ytai

yami, err := flags.GetString("yurt-app-manager-image")
if err != nil {
return err
}
co.YurtAppManagerImage = yami

pmp, err := flags.GetString("pod-manifest-path")
if err != nil {
return err
Expand All @@ -215,6 +236,12 @@ func (co *ConvertOptions) Complete(flags *pflag.FlagSet) error {
return err
}

// parse kubeconfig and generate the yurtappmanagerclientset
co.yurtAppManagerClientSet, err = kubeutil.GenDynamicClientSet(flags)
if err != nil {
return err
}

// prepare path of cluster kubeconfig file
co.kubeConfigPath, err = kubeutil.PrepareKubeConfigPath(flags)
if err != nil {
Expand Down Expand Up @@ -347,7 +374,18 @@ func (co *ConvertOptions) RunConvert() (err error) {
return
}

// 7. deploy yurt-hub and reset the kubelet service
//7. deploy the yurtappmanager if required
if co.EnableAppManager {
if err = deployYurtAppManager(co.clientSet,
co.YurtAppManagerImage,
co.yurtAppManagerClientSet); err != nil {
err = fmt.Errorf("fail to deploy the yurt-app-manager: %s", err)
return
}
klog.Info("yurt-app-manager is deployed")
}

// 8. deploy yurt-hub and reset the kubelet service
klog.Infof("deploying the yurt-hub and resetting the kubelet service...")
joinToken, err := kubeutil.GetOrCreateJoinTokenString(co.clientSet)
if err != nil {
Expand Down Expand Up @@ -377,6 +415,83 @@ func (co *ConvertOptions) RunConvert() (err error) {
return
}

func deployYurtAppManager(
client *kubernetes.Clientset,
yurtappmanagerImage string,
yurtAppManagerClient dynamic.Interface) error {

// 1.create the YurtAppManagerCustomResourceDefinition
// 1.1 nodepool
if err := kubeutil.CreateCRDFromYaml(client, yurtAppManagerClient, "", []byte(constants.YurtAppManagerNodePool)); err != nil {
return err
}

// 1.2 uniteddeployment
if err := kubeutil.CreateCRDFromYaml(client, yurtAppManagerClient, "", []byte(constants.YurtAppManagerUnitedDeployment)); err != nil {
return err
}

// 2. create the YurtAppManagerRole
if err := kubeutil.CreateRoleFromYaml(client, "kube-system",
constants.YurtAppManagerRole); err != nil {
return err
}

// 3. create the ClusterRole
if err := kubeutil.CreateClusterRoleFromYaml(client,
constants.YurtAppManagerClusterRole); err != nil {
return err
}

// 4. create the RoleBinding
if err := kubeutil.CreateRoleBindingFromYaml(client, "kube-system",
constants.YurtAppManagerRolebinding); err != nil {
return err
}

// 5. create the ClusterRoleBinding
if err := kubeutil.CreateClusterRoleBindingFromYaml(client,
constants.YurtAppManagerClusterRolebinding); err != nil {
return err
}

// 6. create the Secret
if err := kubeutil.CreateSecretFromYaml(client, "kube-system",
constants.YurtAppManagerSecret); err != nil {
return err
}

// 7. create the Service
if err := kubeutil.CreateServiceFromYaml(client,
constants.YurtAppManagerService); err != nil {
return err
}

// 8. create the Deployment
if err := kubeutil.CreateDeployFromYaml(client,
"kube-system",
constants.YurtAppManagerDeployment,
map[string]string{
"image": yurtappmanagerImage,
"edgeWorkerLabel": projectinfo.GetEdgeWorkerLabelKey()}); err != nil {
return err
}

// 9. create the YurtAppManagerMutatingWebhookConfiguration
if err := kubeutil.CreateMutatingWebhookConfigurationFromYaml(client,
constants.YurtAppManagerMutatingWebhookConfiguration); err != nil {
return err
}

// 10. create the YurtAppManagerValidatingWebhookConfiguration
if err := kubeutil.CreateValidatingWebhookConfigurationFromYaml(client,
constants.YurtAppManagerValidatingWebhookConfiguration); err != nil {
return err
}

return nil
}

func deployYurttunnelServer(
client *kubernetes.Clientset,
cloudNodes []string,
Expand Down
Loading