You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2020/10/21 07:40:05 UTC

[camel-k] 03/07: API: remove unnecessary packages

This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch api
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 3a178645747dc8a711d88cc451e857eaf9a3da88
Author: Nicola Ferraro <ni...@gmail.com>
AuthorDate: Thu Apr 30 17:50:15 2020 +0200

    API: remove unnecessary packages
---
 pkg/client/client.go     | 256 -----------------------------------------------
 pkg/client/fastmapper.go |  93 -----------------
 2 files changed, 349 deletions(-)

diff --git a/pkg/client/client.go b/pkg/client/client.go
deleted file mode 100644
index 9de66ad..0000000
--- a/pkg/client/client.go
+++ /dev/null
@@ -1,256 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements.  See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License.  You may obtain a copy of the License at
-
-   http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package client
-
-import (
-	"io/ioutil"
-	"os"
-	"path/filepath"
-
-	user "github.com/mitchellh/go-homedir"
-	"github.com/operator-framework/operator-sdk/pkg/k8sutil"
-	"github.com/pkg/errors"
-	"github.com/sirupsen/logrus"
-
-	"k8s.io/apimachinery/pkg/api/meta"
-	"k8s.io/apimachinery/pkg/runtime"
-	"k8s.io/apimachinery/pkg/runtime/schema"
-
-	"k8s.io/client-go/kubernetes"
-	clientscheme "k8s.io/client-go/kubernetes/scheme"
-	"k8s.io/client-go/rest"
-	"k8s.io/client-go/tools/clientcmd"
-	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
-
-	clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
-	controller "sigs.k8s.io/controller-runtime/pkg/client"
-	"sigs.k8s.io/controller-runtime/pkg/client/config"
-	"sigs.k8s.io/controller-runtime/pkg/manager"
-
-	"github.com/apache/camel-k/pkg/apis"
-)
-
-const inContainerNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
-
-// Client is an abstraction for a k8s client
-type Client interface {
-	controller.Client
-	kubernetes.Interface
-	GetScheme() *runtime.Scheme
-	GetConfig() *rest.Config
-	GetCurrentNamespace(kubeConfig string) (string, error)
-}
-
-// Injectable identifies objects that can receive a Client
-type Injectable interface {
-	InjectClient(Client)
-}
-
-// Provider is used to provide a new instance of the Client each time it's required
-type Provider struct {
-	Get func() (Client, error)
-}
-
-type defaultClient struct {
-	controller.Client
-	kubernetes.Interface
-	scheme *runtime.Scheme
-	config *rest.Config
-}
-
-func (c *defaultClient) GetScheme() *runtime.Scheme {
-	return c.scheme
-}
-
-func (c *defaultClient) GetConfig() *rest.Config {
-	return c.config
-}
-
-func (c *defaultClient) GetCurrentNamespace(kubeConfig string) (string, error) {
-	return GetCurrentNamespace(kubeConfig)
-}
-
-// NewOutOfClusterClient creates a new k8s client that can be used from outside the cluster
-func NewOutOfClusterClient(kubeconfig string) (Client, error) {
-	initialize(kubeconfig)
-	// using fast discovery from outside the cluster
-	return NewClient(true)
-}
-
-// NewClient creates a new k8s client that can be used from outside or in the cluster
-func NewClient(fastDiscovery bool) (Client, error) {
-	// Get a config to talk to the apiserver
-	cfg, err := config.GetConfig()
-	if err != nil {
-		return nil, err
-	}
-
-	scheme := clientscheme.Scheme
-
-	// Setup Scheme for all resources
-	if err := apis.AddToScheme(scheme); err != nil {
-		return nil, err
-	}
-
-	var clientset kubernetes.Interface
-	if clientset, err = kubernetes.NewForConfig(cfg); err != nil {
-		return nil, err
-	}
-
-	var mapper meta.RESTMapper
-	if fastDiscovery {
-		mapper = newFastDiscoveryRESTMapper(cfg)
-	}
-
-	// Create a new client to avoid using cache (enabled by default on operator-sdk client)
-	clientOptions := controller.Options{
-		Scheme: scheme,
-		Mapper: mapper,
-	}
-	dynClient, err := controller.New(cfg, clientOptions)
-	if err != nil {
-		return nil, err
-	}
-
-	return &defaultClient{
-		Client:    dynClient,
-		Interface: clientset,
-		scheme:    clientOptions.Scheme,
-		config:    cfg,
-	}, nil
-}
-
-// FromManager creates a new k8s client from a manager object
-func FromManager(manager manager.Manager) (Client, error) {
-	var err error
-	var clientset kubernetes.Interface
-	if clientset, err = kubernetes.NewForConfig(manager.GetConfig()); err != nil {
-		return nil, err
-	}
-	return &defaultClient{
-		Client:    manager.GetClient(),
-		Interface: clientset,
-		scheme:    manager.GetScheme(),
-		config:    manager.GetConfig(),
-	}, nil
-}
-
-// init initialize the k8s client for usage outside the cluster
-func initialize(kubeconfig string) {
-	if kubeconfig == "" {
-		// skip out-of-cluster initialization if inside the container
-		if kc, err := shouldUseContainerMode(); kc && err == nil {
-			return
-		} else if err != nil {
-			logrus.Errorf("could not determine if running in a container: %v", err)
-		}
-		var err error
-		kubeconfig, err = getDefaultKubeConfigFile()
-		if err != nil {
-			panic(err)
-		}
-	}
-	os.Setenv(k8sutil.KubeConfigEnvVar, kubeconfig)
-}
-
-func getDefaultKubeConfigFile() (string, error) {
-	dir, err := user.Dir()
-	if err != nil {
-		return "", err
-	}
-	return filepath.Join(dir, ".kube", "config"), nil
-}
-
-// GetCurrentNamespace --
-func GetCurrentNamespace(kubeconfig string) (string, error) {
-	if kubeconfig == "" {
-		kubeContainer, err := shouldUseContainerMode()
-		if err != nil {
-			return "", err
-		}
-		if kubeContainer {
-			return getNamespaceFromKubernetesContainer()
-		}
-	}
-	if kubeconfig == "" {
-		var err error
-		kubeconfig, err = getDefaultKubeConfigFile()
-		if err != nil {
-			logrus.Errorf("Cannot get information about current user: %v", err)
-		}
-	}
-	if kubeconfig == "" {
-		return "default", nil
-	}
-
-	data, err := ioutil.ReadFile(kubeconfig)
-	if err != nil {
-		return "", err
-	}
-	conf := clientcmdapi.NewConfig()
-	if len(data) == 0 {
-		return "", errors.New("kubernetes config file is empty")
-	}
-
-	decoded, _, err := clientcmdlatest.Codec.Decode(data, &schema.GroupVersionKind{Version: clientcmdlatest.Version, Kind: "Config"}, conf)
-	if err != nil {
-		return "", err
-	}
-
-	clientcmdconfig := decoded.(*clientcmdapi.Config)
-
-	cc := clientcmd.NewDefaultClientConfig(*clientcmdconfig, &clientcmd.ConfigOverrides{})
-	ns, _, err := cc.Namespace()
-	return ns, err
-}
-
-func shouldUseContainerMode() (bool, error) {
-	// When kube config is set, container mode is not used
-	if os.Getenv(k8sutil.KubeConfigEnvVar) != "" {
-		return false, nil
-	}
-	// Use container mode only when the kubeConfigFile does not exist and the container namespace file is present
-	configFile, err := getDefaultKubeConfigFile()
-	if err != nil {
-		return false, err
-	}
-	configFilePresent := true
-	_, err = os.Stat(configFile)
-	if err != nil && os.IsNotExist(err) {
-		configFilePresent = false
-	} else if err != nil {
-		return false, err
-	}
-	if !configFilePresent {
-		_, err := os.Stat(inContainerNamespaceFile)
-		if os.IsNotExist(err) {
-			return false, nil
-		}
-		return true, err
-	}
-	return false, nil
-}
-
-func getNamespaceFromKubernetesContainer() (string, error) {
-	var nsba []byte
-	var err error
-	if nsba, err = ioutil.ReadFile(inContainerNamespaceFile); err != nil {
-		return "", err
-	}
-	return string(nsba), nil
-}
diff --git a/pkg/client/fastmapper.go b/pkg/client/fastmapper.go
deleted file mode 100644
index 82b33b8..0000000
--- a/pkg/client/fastmapper.go
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements.  See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License.  You may obtain a copy of the License at
-
-   http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package client
-
-import (
-	"github.com/sirupsen/logrus"
-	"k8s.io/apimachinery/pkg/api/meta"
-	"k8s.io/apimachinery/pkg/util/wait"
-	"k8s.io/client-go/discovery"
-	"k8s.io/client-go/rest"
-	"k8s.io/client-go/restmapper"
-
-	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-)
-
-// FastMapperAllowedAPIGroups contains a set of API groups that are allowed when using the fastmapper.
-// Those must correspond to all groups used by the "kamel" binary tool when running out-of-cluster.
-var FastMapperAllowedAPIGroups = map[string]bool{
-	"":                          true, // core APIs
-	"apiextensions.k8s.io":      true,
-	"apps":                      true,
-	"batch":                     true,
-	"camel.apache.org":          true,
-	"rbac.authorization.k8s.io": true,
-	"console.openshift.io":      true, // OpenShift console resources
-	"operators.coreos.com":      true, // Operator SDK OLM
-}
-
-// newFastDiscoveryRESTMapper comes from https://github.com/kubernetes-sigs/controller-runtime/pull/592.
-// We may leverage the controller-runtime bits in the future, if that gets merged upstream.
-func newFastDiscoveryRESTMapper(config *rest.Config) meta.RESTMapper {
-	return meta.NewLazyRESTMapperLoader(func() (meta.RESTMapper, error) {
-		return newFastDiscoveryRESTMapperWithFilter(config, func(g *metav1.APIGroup) bool {
-			return FastMapperAllowedAPIGroups[g.Name]
-		})
-	})
-}
-
-func newFastDiscoveryRESTMapperWithFilter(config *rest.Config, filter func(*metav1.APIGroup) bool) (meta.RESTMapper, error) {
-	dc := discovery.NewDiscoveryClientForConfigOrDie(config)
-	groups, err := dc.ServerGroups()
-	if err != nil {
-		return nil, err
-	}
-	wg := wait.Group{}
-	totalCount := 0
-	pickedCount := 0
-	grs := make([]*restmapper.APIGroupResources, 0)
-	for _, group := range groups.Groups {
-		pinnedGroup := group
-		pick := filter(&pinnedGroup)
-		logrus.Debugf("Group: %s %v", group.Name, pick)
-		totalCount++
-		if !pick {
-			continue
-		}
-		pickedCount++
-		gr := &restmapper.APIGroupResources{
-			Group:              group,
-			VersionedResources: make(map[string][]metav1.APIResource),
-		}
-		grs = append(grs, gr)
-		wg.Start(func() { discoverGroupResources(dc, gr) })
-	}
-	wg.Wait()
-	logrus.Debugf("Picked %d/%d", pickedCount, totalCount)
-	return restmapper.NewDiscoveryRESTMapper(grs), nil
-}
-
-func discoverGroupResources(dc discovery.DiscoveryInterface, gr *restmapper.APIGroupResources) {
-	for _, version := range gr.Group.Versions {
-		resources, err := dc.ServerResourcesForGroupVersion(version.GroupVersion)
-		if err != nil {
-			logrus.Fatal(err, version.GroupVersion)
-		}
-		gr.VersionedResources[version.Version] = resources.APIResources
-	}
-}