|
|
@ -17,10 +17,7 @@ limitations under the License.
|
|
|
|
package engine
|
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
@ -28,38 +25,14 @@ import (
|
|
|
|
"k8s.io/client-go/discovery"
|
|
|
|
"k8s.io/client-go/discovery"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var config *rest.Config
|
|
|
|
type lookupFunc = func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error)
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
func NewLookupFunction(config *rest.Config) lookupFunc {
|
|
|
|
// try the out-cluster config, this will default to the in-cluster config is not successful
|
|
|
|
return func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) {
|
|
|
|
var kubeconfig *string
|
|
|
|
|
|
|
|
if home := homeDir(); home != "" {
|
|
|
|
|
|
|
|
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// use the current context in kubeconfig
|
|
|
|
|
|
|
|
config1, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
config = config1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func homeDir() string {
|
|
|
|
|
|
|
|
if h := os.Getenv("HOME"); h != "" {
|
|
|
|
|
|
|
|
return h
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Getenv("USERPROFILE") // windows
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func lookup(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) {
|
|
|
|
|
|
|
|
var client dynamic.ResourceInterface
|
|
|
|
var client dynamic.ResourceInterface
|
|
|
|
c, namespaced, err := getDynamicClientOnKind(apiversion, resource)
|
|
|
|
c, namespaced, err := getDynamicClientOnKind(apiversion, resource, config)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return map[string]interface{}{}, err
|
|
|
|
return map[string]interface{}{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -82,13 +55,20 @@ func lookup(apiversion string, resource string, namespace string, name string) (
|
|
|
|
return map[string]interface{}{}, err
|
|
|
|
return map[string]interface{}{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj.Object, nil
|
|
|
|
return obj.Object, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// func homeDir() string {
|
|
|
|
|
|
|
|
// if h := os.Getenv("HOME"); h != "" {
|
|
|
|
|
|
|
|
// return h
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return os.Getenv("USERPROFILE") // windows
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// GetDynamicClientOnUnstructured returns a dynamic client on an Unstructured type. This client can be further namespaced.
|
|
|
|
// GetDynamicClientOnUnstructured returns a dynamic client on an Unstructured type. This client can be further namespaced.
|
|
|
|
func getDynamicClientOnKind(apiversion string, kind string) (dynamic.NamespaceableResourceInterface, bool, error) {
|
|
|
|
func getDynamicClientOnKind(apiversion string, kind string, config *rest.Config) (dynamic.NamespaceableResourceInterface, bool, error) {
|
|
|
|
gvk := schema.FromAPIVersionAndKind(apiversion, kind)
|
|
|
|
gvk := schema.FromAPIVersionAndKind(apiversion, kind)
|
|
|
|
apiRes, err := getAPIReourceForGVK(gvk)
|
|
|
|
apiRes, err := getAPIReourceForGVK(gvk, config)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] unable to get apiresource from unstructured: %s , error %s", gvk.String(), err)
|
|
|
|
log.Printf("[ERROR] unable to get apiresource from unstructured: %s , error %s", gvk.String(), err)
|
|
|
|
return nil, false, err
|
|
|
|
return nil, false, err
|
|
|
@ -107,7 +87,7 @@ func getDynamicClientOnKind(apiversion string, kind string) (dynamic.Namespaceab
|
|
|
|
return res, apiRes.Namespaced, nil
|
|
|
|
return res, apiRes.Namespaced, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getAPIReourceForGVK(gvk schema.GroupVersionKind) (metav1.APIResource, error) {
|
|
|
|
func getAPIReourceForGVK(gvk schema.GroupVersionKind, config *rest.Config) (metav1.APIResource, error) {
|
|
|
|
res := metav1.APIResource{}
|
|
|
|
res := metav1.APIResource{}
|
|
|
|
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
|
|
|
|
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|