1 # Copyright ETSI Contributors and Others.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
17 from osmclient
.cli_commands
import utils
18 from prettytable
import PrettyTable
23 logger
= logging
.getLogger("osmclient")
26 @click.command(name
="k8scluster-add", short_help
="adds a K8s cluster to OSM")
27 @click.argument("name")
29 "--creds", prompt
=True, help="credentials file, i.e. a valid `.kube/config` file"
31 @click.option("--version", prompt
=True, help="Kubernetes version")
33 "--vim", prompt
=True, help="VIM target, the VIM where the cluster resides"
38 help='''list of VIM networks, in JSON inline format, where the cluster is
39 accessible via L3 routing, e.g. "{(k8s_net1:vim_network1) [,(k8s_net2:vim_network2) ...]}"''',
42 "--init-helm3/--skip-helm3",
45 help="Initialize helm v3",
48 "--init-jujubundle/--skip-jujubundle",
51 help="Initialize juju-bundle",
53 @click.option("--description", default
=None, help="human readable description")
56 default
="kube-system",
57 help="namespace to be used for its operation, defaults to `kube-system`",
64 help="do not return the control immediately, but keep it "
65 "until the operation is completed, or timeout",
70 help="list of CNI plugins, in JSON inline format, used in the cluster",
72 # @click.option('--skip-init',
74 # help='If set, K8s cluster is assumed to be ready for its use with OSM')
90 """adds a K8s cluster to OSM
92 NAME: name of the K8s cluster
94 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
96 cluster
["name"] = name
97 with
open(creds
, "r") as cf
:
98 cluster
["credentials"] = yaml
.safe_load(cf
.read())
99 cluster
["k8s_version"] = version
100 cluster
["vim_account"] = vim
101 cluster
["nets"] = yaml
.safe_load(k8s_nets
)
102 if not (init_jujubundle
and init_helm3
):
103 cluster
["deployment_methods"] = {
104 "juju-bundle": init_jujubundle
,
105 "helm-chart-v3": init_helm3
,
108 cluster
["description"] = description
110 cluster
["namespace"] = namespace
112 cluster
["cni"] = yaml
.safe_load(cni
)
113 ctx
.obj
.k8scluster
.create(name
, cluster
, wait
)
116 @click.command(name
="k8scluster-update", short_help
="updates a K8s cluster")
117 @click.argument("name")
118 @click.option("--newname", help="New name for the K8s cluster")
119 @click.option("--creds", help="credentials file, i.e. a valid `.kube/config` file")
120 @click.option("--version", help="Kubernetes version")
121 @click.option("--vim", help="VIM target, the VIM where the cluster resides")
124 help='''list of VIM networks, in JSON inline format, where the cluster is accessible
125 via L3 routing, e.g. "{(k8s_net1:vim_network1) [,(k8s_net2:vim_network2) ...]}"''',
127 @click.option("--description", help="human readable description")
130 help="namespace to be used for its operation, defaults to `kube-system`",
137 help="do not return the control immediately, but keep it "
138 "until the operation is completed, or timeout",
141 "--cni", help="list of CNI plugins, in JSON inline format, used in the cluster"
144 def k8scluster_update(
145 ctx
, name
, newname
, creds
, version
, vim
, k8s_nets
, description
, namespace
, wait
, cni
147 """updates a K8s cluster
149 NAME: name or ID of the K8s cluster
151 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
154 cluster
["name"] = newname
156 with
open(creds
, "r") as cf
:
157 cluster
["credentials"] = yaml
.safe_load(cf
.read())
159 cluster
["k8s_version"] = version
161 cluster
["vim_account"] = vim
163 cluster
["nets"] = yaml
.safe_load(k8s_nets
)
165 cluster
["description"] = description
167 cluster
["namespace"] = namespace
169 cluster
["cni"] = yaml
.safe_load(cni
)
170 ctx
.obj
.k8scluster
.update(name
, cluster
, wait
)
173 @click.command(name
="k8scluster-delete", short_help
="deletes a K8s cluster")
174 @click.argument("name")
176 "--force", is_flag
=True, help="forces the deletion from the DB (not recommended)"
183 help="do not return the control immediately, but keep it "
184 "until the operation is completed, or timeout",
187 def k8scluster_delete(ctx
, name
, force
, wait
):
188 """deletes a K8s cluster
190 NAME: name or ID of the K8s cluster to be deleted
192 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
193 ctx
.obj
.k8scluster
.delete(name
, force
, wait
)
196 @click.command(name
="k8scluster-list")
201 help="restricts the list to the K8s clusters matching the filter",
203 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
204 @click.option("--long", is_flag
=True, help="get more details")
206 def k8scluster_list(ctx
, filter, literal
, long):
207 """list all K8s clusters"""
208 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
210 filter = "&".join(filter)
211 resp
= ctx
.obj
.k8scluster
.list(filter)
213 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
224 "Deployment methods",
226 "Op. state (details)",
231 project_list
= ctx
.obj
.project
.list()
234 ["Name", "Id", "VIM", "Operational State", "Op. state details"]
237 vim_list
= ctx
.obj
.vim
.list()
241 logger
.debug("Cluster details: {}".format(yaml
.safe_dump(cluster
)))
242 vim_name
= utils
.get_vim_name(vim_list
, cluster
["vim_account"])
243 # vim_info = '{} ({})'.format(vim_name,cluster['vim_account'])
245 op_state_details
= "Helm: {}\nJuju: {}".format(
246 cluster
["_admin"].get("helm-chart", {}).get("operationalState", "-"),
247 cluster
["_admin"].get("juju-bundle", {}).get("operationalState", "-"),
250 project_id
, project_name
= utils
.get_project(project_list
, cluster
)
251 # project_info = '{} ({})'.format(project_name, project_id)
252 project_info
= project_name
253 detailed_status
= cluster
["_admin"].get("detailed-status", "-")
259 cluster
["k8s_version"],
261 json
.dumps(cluster
["nets"]),
262 json
.dumps(cluster
["deployment_methods"]),
263 cluster
["_admin"]["operationalState"],
265 utils
.trunc_text(cluster
.get("description") or "", 40),
266 utils
.wrap_text(text
=detailed_status
, width
=40),
275 cluster
["_admin"]["operationalState"],
283 @click.command(name
="k8scluster-show", short_help
="shows the details of a K8s cluster")
284 @click.argument("name")
285 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
287 def k8scluster_show(ctx
, name
, literal
):
288 """shows the details of a K8s cluster
290 NAME: name or ID of the K8s cluster
292 resp
= ctx
.obj
.k8scluster
.get(name
)
294 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
296 table
= PrettyTable(["key", "attribute"])
297 for k
, v
in list(resp
.items()):
298 table
.add_row([k
, utils
.wrap_text(text
=json
.dumps(v
, indent
=2), width
=100)])