diff --git a/pkg/platform/provider/baremetal/util/client_go.go b/pkg/platform/provider/baremetal/util/client_go.go deleted file mode 100644 index 5c60122dc..000000000 --- a/pkg/platform/provider/baremetal/util/client_go.go +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making TKEStack - * available. - * - * Copyright (C) 2012-2019 Tencent. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use - * this file except in compliance with the License. You may obtain a copy of the - * License at - * - * https://opensource.org/licenses/Apache-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package util - -import ( - "github.com/pkg/errors" - appsv1 "k8s.io/api/apps/v1" - corev1 "k8s.io/api/core/v1" - rbacv1 "k8s.io/api/rbac/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - clientset "k8s.io/client-go/kubernetes" -) - -// CreateOrRetainConfigMap creates a ConfigMap if the target resource doesn't exist. If the resource exists already, this function will retain the resource instead. -func CreateOrRetainConfigMap(client clientset.Interface, cm *corev1.ConfigMap, configMapName string) error { - if _, err := client.CoreV1().ConfigMaps(cm.ObjectMeta.Namespace).Get(configMapName, metav1.GetOptions{}); err != nil { - if !apierrors.IsNotFound(err) { - return nil - } - if _, err := client.CoreV1().ConfigMaps(cm.ObjectMeta.Namespace).Create(cm); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create configmap") - } - } - } - return nil -} - -// CreateOrUpdateConfigMap creates a ConfigMap if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateConfigMap(client clientset.Interface, cm *corev1.ConfigMap) error { - if _, err := client.CoreV1().ConfigMaps(cm.ObjectMeta.Namespace).Create(cm); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create configmap") - } - - if _, err := client.CoreV1().ConfigMaps(cm.ObjectMeta.Namespace).Update(cm); err != nil { - return errors.Wrap(err, "unable to update configmap") - } - } - return nil -} - -// CreateOrUpdateSecret creates a Secret if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateSecret(client clientset.Interface, secret *corev1.Secret) error { - if _, err := client.CoreV1().Secrets(secret.ObjectMeta.Namespace).Create(secret); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create secret") - } - - if _, err := client.CoreV1().Secrets(secret.ObjectMeta.Namespace).Update(secret); err != nil { - return errors.Wrap(err, "unable to update secret") - } - } - return nil -} - -// CreateOrUpdateServiceAccount creates a ServiceAccount if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateServiceAccount(client clientset.Interface, sa *corev1.ServiceAccount) error { - if _, err := client.CoreV1().ServiceAccounts(sa.ObjectMeta.Namespace).Create(sa); err != nil { - // Note: We don't run .Update here afterwards as that's probably not required - // Only thing that could be updated is annotations/labels in .metadata, but we don't use that currently - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create serviceaccount") - } - } - return nil -} - -// CreateOrUpdateRoleBinding creates a RoleBinding if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateRoleBinding(client clientset.Interface, roleBinding *rbacv1.RoleBinding) error { - if _, err := client.RbacV1().RoleBindings(roleBinding.ObjectMeta.Namespace).Create(roleBinding); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create RBAC rolebinding") - } - - if _, err := client.RbacV1().RoleBindings(roleBinding.ObjectMeta.Namespace).Update(roleBinding); err != nil { - return errors.Wrap(err, "unable to update RBAC rolebinding") - } - } - return nil -} - -// CreateOrUpdateClusterRole creates a ClusterRole if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateClusterRole(client clientset.Interface, clusterRole *rbacv1.ClusterRole) error { - if _, err := client.RbacV1().ClusterRoles().Create(clusterRole); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create RBAC clusterrole") - } - - if _, err := client.RbacV1().ClusterRoles().Update(clusterRole); err != nil { - return errors.Wrap(err, "unable to update RBAC clusterrole") - } - } - return nil -} - -// CreateOrUpdateClusterRoleBinding creates a ClusterRoleBinding if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateClusterRoleBinding(client clientset.Interface, clusterRoleBinding *rbacv1.ClusterRoleBinding) error { - if _, err := client.RbacV1().ClusterRoleBindings().Create(clusterRoleBinding); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create RBAC clusterrolebinding") - } - - if _, err := client.RbacV1().ClusterRoleBindings().Update(clusterRoleBinding); err != nil { - return errors.Wrap(err, "unable to update RBAC clusterrolebinding") - } - } - return nil -} - -// CreateOrUpdateDeployment creates a Deployment if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateDeployment(client clientset.Interface, deploy *appsv1.Deployment) error { - if _, err := client.AppsV1().Deployments(deploy.ObjectMeta.Namespace).Create(deploy); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create deployment") - } - - if _, err := client.AppsV1().Deployments(deploy.ObjectMeta.Namespace).Update(deploy); err != nil { - return errors.Wrap(err, "unable to update deployment") - } - } - return nil -} - -// CreateOrUpdateDaemonSet creates a DaemonSet if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func CreateOrUpdateDaemonSet(client clientset.Interface, deploy *appsv1.DaemonSet) error { - if _, err := client.AppsV1().DaemonSets(deploy.ObjectMeta.Namespace).Create(deploy); err != nil { - if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "unable to create daemonset") - } - - if _, err := client.AppsV1().DaemonSets(deploy.ObjectMeta.Namespace).Update(deploy); err != nil { - return errors.Wrap(err, "unable to update daemonset") - } - } - return nil -} diff --git a/pkg/platform/provider/baremetal/util/host.go b/pkg/platform/provider/baremetal/util/host.go deleted file mode 100644 index eea316362..000000000 --- a/pkg/platform/provider/baremetal/util/host.go +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making TKEStack - * available. - * - * Copyright (C) 2012-2019 Tencent. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use - * this file except in compliance with the License. You may obtain a copy of the - * License at - * - * https://opensource.org/licenses/Apache-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package util - -import ( - "os" - "strings" - - "io/ioutil" - netutil "k8s.io/apimachinery/pkg/util/net" - "net" - "path" -) - -func HostName() (string, error) { - nodeName, err := os.Hostname() - if err != nil { - return "", err - } - nodeName = strings.ToLower(nodeName) - return nodeName, nil -} - -func HostIP() (net.IP, error) { - nodeIP, err := netutil.ChooseHostInterface() - if err != nil { - return net.IP{}, err - } - return nodeIP, nil -} - -func WriteFile(p string, data []byte, perm os.FileMode) error { - if _, err := os.Stat(path.Dir(p)); err != nil { - if os.IsNotExist(err) { - if err := os.MkdirAll(path.Dir(p), 0755); err != nil { - return err - } - } - } - return ioutil.WriteFile(p, data, perm) -} diff --git a/pkg/platform/provider/baremetal/util/journal.go b/pkg/platform/provider/baremetal/util/journal.go deleted file mode 100644 index 6a8a32b79..000000000 --- a/pkg/platform/provider/baremetal/util/journal.go +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making TKEStack - * available. - * - * Copyright (C) 2012-2019 Tencent. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use - * this file except in compliance with the License. You may obtain a copy of the - * License at - * - * https://opensource.org/licenses/Apache-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package util - -func ReadSystemdUnitLog(unitName string, line uint64) ([]byte, error) { - //r, err := sdjournal.NewJournalReader(sdjournal.JournalReaderConfig{ - // NumFromTail: line, - // Matches: []sdjournal.Match{ - // { - // Field: "_SYSTEMD_UNIT", - // Value: fmt.Sprintf(`%s`, unitName), - // }, - // }, - // Formatter: func(entry *sdjournal.JournalEntry) (string, error) { - // return fmt.Sprintf("%s\n", entry.Fields["MESSAGE"]), nil - // }, - //}) - //if err != nil { - // return []byte{}, nil - //} - // - //if r == nil { - // return []byte{}, errors.New("") - //} - // - //defer r.Close() - // - //buf := new(bytes.Buffer) - //var e error - //for c := -1; c != 0 && e == nil; { - // tmpBuf := make([]byte, 5) - // c, e = r.Read(tmpBuf) - // if c > len(tmpBuf) { - // return []byte{}, errors.New(fmt.Sprintf("Got unexpected read length: %d vs %d", c, len(tmpBuf))) - // } - // _, _ = buf.Write(tmpBuf) - //} - - return []byte{}, nil -}