AIO and MUNO mode upgrade for EG 1.5.0 version
[eliot.git] / blueprints / common / elcli / elcli / cmd / util / k8sinstaller.go
1 /*
2 Copyright 2019 The Eliot Team.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package util
18
19 import (
20         "fmt"
21
22         types "elcli/cmd/common"
23 )
24
25
26 //K8SInstTool embedes Common struct and contains the default K8S version and
27 //a flag depicting if host is an edge or cloud node
28 //It implements ToolsInstaller interface
29 type K8SInstTool struct {
30         Common
31         IsEdgeNode     bool //True - Edgenode False - Cloudnode
32         DefaultToolVer string
33 }
34
35
36 //InstallTools sets the OS interface, checks if K8S installation is required or not.
37 //If required then install the said version.
38 func (ks *K8SInstTool) InstallTools() error {
39         ks.SetOSInterface(GetOSInterface())
40         ks.SetK8SVersionAndIsNodeFlag(ks.ToolVersion, ks.IsEdgeNode)
41
42         component := "kubeadm"
43         if ks.IsEdgeNode == true {
44                 component = "kubectl"
45         }
46         action, err := ks.IsK8SComponentInstalled(component, ks.DefaultToolVer)
47         if err != nil {
48                 return err
49         }
50         switch action {
51         case types.VersionNAInRepo:
52                 return fmt.Errorf("Expected %s version is not available in OS repo", component)
53         case types.AlreadySameVersionExist:
54                 fmt.Printf("Same version of %s already installed in this host", component)
55                 return nil
56         case types.DefVerInstallRequired:
57                 ks.SetK8SVersionAndIsNodeFlag(ks.DefaultToolVer, ks.IsEdgeNode)
58                 fallthrough
59         case types.NewInstallRequired:
60                 err := ks.InstallK8S()
61                 if err != nil {
62                         return err
63                 }
64         default:
65                 return fmt.Errorf("Error in getting the %s version from host", component)
66         }
67         return nil
68 }
69
70 //TearDown shoud uninstall K8S, but it is not required either for cloud or edge node.
71 //It is defined so that K8SInstTool implements ToolsInstaller interface
72 func (ks *K8SInstTool) TearDown() error {
73         return nil
74 }