How to use NewAPIConfig method of client Package

Best Testkube code snippet using client.NewAPIConfig

k8s.go

Source:k8s.go Github

copy

Full Screen

1// Copyright 2021 l1b0k2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14package utils15import (16 "fmt"17 "net"18 "os"19 "path/filepath"20 "strings"21 "github.com/spf13/pflag"22 "github.com/spf13/viper"23 corev1 "k8s.io/api/core/v1"24 "k8s.io/client-go/kubernetes"25 "k8s.io/client-go/tools/clientcmd"26 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"27)28func init() {29 pflag.String("kubeconfig", "", "Paths to a kubeconfig. Only required if out-of-cluster.")30}31func ClientFromKubeConfig(conf []byte) (kubernetes.Interface, error) {32 rest, err := clientcmd.RESTConfigFromKubeConfig(conf)33 if err != nil {34 return nil, err35 }36 return kubernetes.NewForConfig(rest)37}38func ClientOrDie() kubernetes.Interface {39 rest, err := clientcmd.BuildConfigFromFlags("", viper.GetString("kubeconfig"))40 if err != nil {41 fmt.Printf("failed build k8s client, %v", err)42 os.Exit(1)43 }44 return kubernetes.NewForConfigOrDie(rest)45}46func MergeApiConfig(newAPIConfig *clientcmdapi.Config, override bool) (*clientcmdapi.Config, error) {47 path := viper.GetString("kubeconfig")48 _, err := os.Stat(filepath.Dir(path))49 if err != nil {50 if !os.IsNotExist(err) {51 return nil, fmt.Errorf("error read %s ,%w", path, err)52 }53 _ = os.MkdirAll(filepath.Dir(path), 0750)54 }55 _, err = os.Stat(path)56 if err != nil {57 if !os.IsNotExist(err) {58 return nil, fmt.Errorf("error read %s ,%w", path, err)59 }60 _ = os.WriteFile(path, nil, os.ModePerm)61 }62 oldApiConfig, err := clientcmd.LoadFromFile(path)63 if err != nil {64 return nil, err65 }66 newCtx, ok := newAPIConfig.Contexts[newAPIConfig.CurrentContext]67 if !ok {68 return nil, fmt.Errorf("error get ctx from config")69 }70 _, ok = oldApiConfig.Clusters[newCtx.Cluster]71 if ok && !override {72 return nil, fmt.Errorf("conflict clister %s", newCtx.Cluster)73 }74 oldApiConfig.Clusters[newCtx.Cluster] = newAPIConfig.Clusters[newCtx.Cluster]75 oldApiConfig.AuthInfos[newCtx.AuthInfo] = newAPIConfig.AuthInfos[newCtx.AuthInfo]76 oldApiConfig.Contexts[newAPIConfig.CurrentContext] = newCtx77 oldApiConfig.CurrentContext = newAPIConfig.CurrentContext78 err = clientcmd.WriteToFile(*oldApiConfig, path)79 if err != nil {80 return nil, err81 }82 return clientcmd.LoadFromFile(path)83}84func SetKubeConfigName(conf []byte, clusterID, name string) (*clientcmdapi.Config, error) {85 apiConfig, err := clientcmd.Load(conf)86 if err != nil {87 return nil, err88 }89 oldCtx, ok := apiConfig.Contexts[apiConfig.CurrentContext]90 if !ok {91 return nil, fmt.Errorf("error get context %s form kubeconfig", apiConfig.CurrentContext)92 }93 oldCluster, ok := apiConfig.Clusters[oldCtx.Cluster]94 if !ok {95 return nil, fmt.Errorf("error get cluster %s form kubeconfig", oldCtx.Cluster)96 }97 oldAuthInfo, ok := apiConfig.AuthInfos[oldCtx.AuthInfo]98 if !ok {99 return nil, fmt.Errorf("error get authinfo %s form kubeconfig", oldCtx.AuthInfo)100 }101 cfg := clientcmdapi.NewConfig()102 cfg.CurrentContext = name103 newCtx := clientcmdapi.NewContext()104 newCtx.Cluster = fmt.Sprintf("cluster-%s", clusterID)105 newCtx.AuthInfo = fmt.Sprintf("user-%s", clusterID)106 newCtx.Namespace = oldCtx.Namespace107 cfg.Contexts[name] = newCtx108 cfg.Clusters[fmt.Sprintf("cluster-%s", clusterID)] = oldCluster109 cfg.AuthInfos[fmt.Sprintf("user-%s", clusterID)] = oldAuthInfo110 return cfg, nil111}112func ParseInstanceID(node *corev1.Node) string {113 keys := strings.Split(node.Spec.ProviderID, ".")114 if len(keys) > 1 {115 return keys[1]116 }117 return ""118}119func ParseInternalIP(node *corev1.Node) net.IP {120 for _, a := range node.Status.Addresses {121 if a.Type != corev1.NodeInternalIP {122 continue123 }124 return net.ParseIP(a.Address)125 }126 return nil127}128func ParseRegion(node *corev1.Node) string {129 id, _ := node.Labels[corev1.LabelTopologyRegion]130 return id131}132func ParseZone(node *corev1.Node) string {133 id, _ := node.Labels[corev1.LabelTopologyZone]134 return id135}136func ParseInstanceType(node *corev1.Node) string {137 id, _ := node.Labels[corev1.LabelInstanceTypeStable]138 return id139}...

Full Screen

Full Screen

config.go

Source:config.go Github

copy

Full Screen

...17 Password string18 DBName string19 }20}21// NewAPIConfig ...22func NewAPIConfig() *APIConfig {23 config := new(APIConfig)24 config.HeaderValue.ClientID = os.Getenv("SBCNTR_CLIENT_ID_HEADER")25 return config26}27// NewConfigDB ...28func NewConfigDB() *ConfigDB {29 config := new(ConfigDB)30 //os.Setenv("DB_HOST", "localhost")31 //os.Setenv("DB_USERNAME", "sbcntruser")32 //os.Setenv("DB_PASSWORD", "sbcntrpass")33 //os.Setenv("DB_NAME", "sbcntrapp")34 config.MySQL.DBMS = "mysql"35 config.MySQL.Protocol = "tcp(" + os.Getenv("DB_HOST") + ":3306)"36 config.MySQL.Username = os.Getenv("DB_USERNAME")...

Full Screen

Full Screen

NewAPIConfig

Using AI Code Generation

copy

Full Screen

1import (2type MyPlugin struct {3}4func main() {5 plugin.Start(new(MyPlugin))6}7func (p *MyPlugin) Run(context plugin.PluginContext, args []string) {8 config := httputil.NewAPIConfig()9}10import (

Full Screen

Full Screen

NewAPIConfig

Using AI Code Generation

copy

Full Screen

1import (2func NewAPIConfig(args []string) (plugin.Config, error) {3 return plugin.NewAPIConfig(args, plugin.NewCommandMetadata("test", "test", "test"))4}5func NewPlugin() plugin.Plugin {6 return &Plugin{}7}8type Plugin struct {9}10func (p *Plugin) GetMetadata() plugin.PluginMetadata {11 return plugin.PluginMetadata{12 }13}14func (p *Plugin) Run(context plugin.PluginContext, args []string) {15 t := terminal.NewUI()16 i18n := i18n.NewI18n("en-US")17 util := util.NewUtil(t, i18n)18 u := ui.NewUI(t, i18n, util)19 c := clients.NewClient(u)20 fmt.Println(c.GetAPIConfig().BluemixAPIEndpoint())21}22func main() {23 plugin.Start(NewAPIConfig, NewPlugin)24}25import (26func NewAPIConfig(args

Full Screen

Full Screen

NewAPIConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 config, err := clientcmd.BuildConfigFromFlags("", "/home/centos/.kube/config")4 if err != nil {5 panic(err)6 }7 oc, err := client.New(config)8 if err != nil {9 panic(err)10 }11 fmt.Println(oc)12 fmt.Println("Config", config)13}14import (15func main() {16 config, err := clientcmd.BuildConfigFromFlags("", "/home/centos/.kube/config")17 if err != nil {18 panic(err)19 }20 oc, err := client.New(config)21 if err != nil {22 panic(err)23 }24 fmt.Println(oc)25 fmt.Println("Config", config)26}27import (28func main() {29 config, err := clientcmd.BuildConfigFromFlags("", "/home/centos/.kube/config")30 if err != nil {31 panic(err)32 }33 oc, err := client.New(config)34 if err != nil {35 panic(err)36 }37 fmt.Println(oc)

Full Screen

Full Screen

NewAPIConfig

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 projectID, err := metadata.ProjectID()4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope)9 if err != nil {10 fmt.Println(err)11 os.Exit(1)12 }13 computeService, err := compute.New(client)14 if err != nil {15 fmt.Println(err)16 os.Exit(1)17 }18 instances, err := computeService.Instances.List(projectID).Do()19 if err != nil {20 fmt.Println(err)21 os.Exit(1)22 }23 for _, instance := range instances.Items {24 fmt.Println(instance.Name)25 }26}27import (28func main() {29 projectID, err := metadata.ProjectID()30 if err != nil {31 fmt.Println(err)32 os.Exit(1)33 }34 client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope)35 if err != nil {36 fmt.Println(err)37 os.Exit(1)38 }39 computeService, err := compute.New(client)40 if err != nil {41 fmt.Println(err)42 os.Exit(1)43 }44 instances, err := computeService.Instances.List(projectID).Do()45 if err != nil {46 fmt.Println(err)47 os.Exit(1)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful