forked from openshift/cloud-credential-operator
/
platform.go
62 lines (52 loc) · 2.02 KB
/
platform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package platform
import (
"context"
configv1 "github.com/openshift/api/config/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// GetInfraStatusUsingKubeconfig queries the k8s api for the infrastructure CR using the kubeconfig file
// pointed to by the passed in kubeconfig (pass in empty string to use default k8s client configurations)
func GetInfraStatusUsingKubeconfig(m manager.Manager, kubeconfig string) (*configv1.InfrastructureStatus, error) {
c, err := getClient(kubeconfig)
if err != nil {
return nil, err
}
return GetInfraStatus(c)
}
// GetInfraStatus will return the clusterwide Infrastructure's object status
func GetInfraStatus(kClient client.Client) (*configv1.InfrastructureStatus, error) {
infra := &configv1.Infrastructure{}
infraName := types.NamespacedName{Name: "cluster"}
if err := kClient.Get(context.Background(), infraName, infra); err != nil {
return nil, err
}
return &infra.Status, nil
}
// GetType returns the platform type given an infrastructure status. If PlatformStatus is set,
// it will get the platform type from it, otherwise it will get it from InfraStatus.Platform which
// is deprecated in 4.2
func GetType(infraStatus *configv1.InfrastructureStatus) configv1.PlatformType {
//if infraStatus.PlatformStatus != nil && len(infraStatus.PlatformStatus.Type) > 0 {
// return infraStatus.PlatformStatus.Type
//}
return configv1.AWSPlatformType
//return infraStatus.Platform
}
func getClient(explicitKubeconfig string) (client.Client, error) {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = explicitKubeconfig
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})
cfg, err := kubeconfig.ClientConfig()
if err != nil {
return nil, err
}
//apis.AddToScheme(scheme.Scheme)
dynamicClient, err := client.New(cfg, client.Options{})
if err != nil {
return nil, err
}
return dynamicClient, nil
}