在本地启动k8s工作区
(cmd *cobra.Command, k8sUtil k8s.KubernetesUtil, workspaceInfo workspace.WorkspaceInfo, yamlExecuteFun func(yamlConfig config.SmartIdeK8SConfig, workspaceInfo workspace.WorkspaceInfo, cmdtype, userguid, workspaceid string))
| 36 | |
| 37 | // 在本地启动k8s工作区 |
| 38 | func ExecuteK8s_LocalWS_LocalEnv(cmd *cobra.Command, k8sUtil k8s.KubernetesUtil, |
| 39 | workspaceInfo workspace.WorkspaceInfo, |
| 40 | yamlExecuteFun func(yamlConfig config.SmartIdeK8SConfig, workspaceInfo workspace.WorkspaceInfo, cmdtype, userguid, workspaceid string)) (workspace.WorkspaceInfo, error) { |
| 41 | |
| 42 | needStore := false |
| 43 | if workspaceInfo.ID == "" { |
| 44 | needStore = true |
| 45 | } |
| 46 | |
| 47 | //1. create namespace |
| 48 | _, err := k8sUtil.ExecKubectlCommandCombined(" get namespace "+k8sUtil.Namespace, "") |
| 49 | if _, isExitError := err.(*exec.ExitError); isExitError { // 如果不存在,才需要创建 |
| 50 | needStore = true |
| 51 | common.SmartIDELog.Info("create namespace:" + k8sUtil.Namespace) |
| 52 | |
| 53 | labels := getK8sLabels(cmd, workspaceInfo) |
| 54 | // namespace |
| 55 | namespaceKind := coreV1.Namespace{} |
| 56 | namespaceKind.Kind = "Namespace" // 必须要赋值,否则为空 |
| 57 | namespaceKind.APIVersion = "v1" // 必须要赋值,否则为空 |
| 58 | namespaceKind.ObjectMeta.Name = k8sUtil.Namespace |
| 59 | namespaceKind = k8s.AddLabels(namespaceKind, labels).(coreV1.Namespace) |
| 60 | |
| 61 | // 创建文件 |
| 62 | // home 目录的路径 |
| 63 | home, err := os.UserHomeDir() |
| 64 | if err != nil { |
| 65 | return workspaceInfo, err |
| 66 | } |
| 67 | workingRootDir := filepath.Join(home, ".ide", ".k8s") // 工作目录,repo 会clone到当前目录下 |
| 68 | workspaceInfo.WorkingDirectoryPath = workingRootDir // 赋值避免出错 |
| 69 | gitRepoRootDirPath := filepath.Join(workingRootDir, common.GetRepoName(workspaceInfo.GitCloneRepoUrl)) |
| 70 | err = os.MkdirAll(gitRepoRootDirPath, os.ModePerm) |
| 71 | if err != nil { |
| 72 | return workspaceInfo, err |
| 73 | } |
| 74 | tempK8sNamespaceYamlAbsolutePath := filepath.Join(gitRepoRootDirPath, fmt.Sprintf("k8s_deployment_%v_temp_namespace.yaml", filepath.Base(gitRepoRootDirPath))) |
| 75 | k8sYamlContent, err := config.ConvertK8sKindToString(namespaceKind) |
| 76 | if err != nil { |
| 77 | return workspaceInfo, err |
| 78 | } |
| 79 | err = os.WriteFile(tempK8sNamespaceYamlAbsolutePath, []byte(k8sYamlContent), 0777) |
| 80 | if err != nil { |
| 81 | return workspaceInfo, err |
| 82 | } |
| 83 | |
| 84 | // apply |
| 85 | err = k8sUtil.ExecKubectlCommandRealtime(fmt.Sprintf("apply -f %v", tempK8sNamespaceYamlAbsolutePath), "", false) |
| 86 | if err != nil { |
| 87 | return workspaceInfo, err |
| 88 | } |
| 89 | |
| 90 | // set value |
| 91 | workspaceInfo.K8sInfo.Namespace = k8sUtil.Namespace |
| 92 | } |
| 93 | |
| 94 | //2. store |
| 95 | if needStore { |
nothing calls this directly
no test coverage detected