Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 afrouterd |
| 18 | |
| 19 | import ( |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 20 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 21 | "k8s.io/client-go/kubernetes" |
| 22 | "k8s.io/client-go/rest" |
| 23 | "k8s.io/client-go/tools/clientcmd" |
| 24 | ) |
| 25 | |
| 26 | func K8sClientSet() *kubernetes.Clientset { |
| 27 | var config *rest.Config |
| 28 | if k8sApiServer != "" || k8sKubeConfigPath != "" { |
| 29 | // use combination of URL & local kube-config file |
| 30 | c, err := clientcmd.BuildConfigFromFlags(k8sApiServer, k8sKubeConfigPath) |
| 31 | if err != nil { |
| 32 | panic(err) |
| 33 | } |
| 34 | config = c |
| 35 | } else { |
| 36 | // use in-cluster config |
| 37 | c, err := rest.InClusterConfig() |
| 38 | if err != nil { |
| 39 | log.Errorf("Unable to load in-cluster config. Try setting K8S_API_SERVER and K8S_KUBE_CONFIG_PATH?") |
| 40 | panic(err) |
| 41 | } |
| 42 | config = c |
| 43 | } |
| 44 | // creates the clientset |
| 45 | clientset, err := kubernetes.NewForConfig(config) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | |
| 50 | return clientset |
| 51 | } |