This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lay the foundations for the kubernetes provider (#621)
This commit: * Adds the provider struct and the empty functions for discovering and scanning. * Creates a kubernetes client for accessing the kubernetes API * Adds a configuration loader which looks for environment variables prefixed with VMCLARITY_KUBERNETES * Adds the Kubernetes provider type to the api spec * Updates uibackend to handle the kubernetes provider type * Sdds location field to the container asset type which can be used by the provider to keep track of where a container was found * Support kubernetes provider configuration in helm-chart
- Loading branch information
Showing
15 changed files
with
386 additions
and
153 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// Licensed 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 kubernetes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
DefaultEnvPrefix = "VMCLARITY_KUBERNETES" | ||
) | ||
|
||
type Config struct { | ||
// KubeConfig defines a path to a kubeconfig file to use to connect to | ||
// the Kubernetes API | ||
KubeConfig string `mapstructure:"kubeconfig"` | ||
} | ||
|
||
func NewConfig() (*Config, error) { | ||
// Avoid modifying the global instance | ||
v := viper.New() | ||
|
||
v.SetEnvPrefix(DefaultEnvPrefix) | ||
v.AllowEmptyEnv(true) | ||
v.AutomaticEnv() | ||
|
||
_ = v.BindEnv("kubeconfig") | ||
|
||
config := &Config{} | ||
if err := v.Unmarshal(config); err != nil { | ||
return nil, fmt.Errorf("failed to parse provider configuration. Provider=Kubernetes: %w", err) | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// Licensed 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 kubernetes | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/openclarity/vmclarity/api/models" | ||
"github.com/openclarity/vmclarity/pkg/orchestrator/provider" | ||
) | ||
|
||
type Provider struct { | ||
clientset kubernetes.Interface | ||
config *Config | ||
} | ||
|
||
var _ provider.Provider = &Provider{} | ||
|
||
func New(ctx context.Context) (provider.Provider, error) { | ||
config, err := NewConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid configuration: %w", err) | ||
} | ||
|
||
var clientConfig *rest.Config | ||
if config.KubeConfig == "" { | ||
// If KubeConfig config option not set, assume we're running | ||
// incluster. | ||
clientConfig, err = rest.InClusterConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to load in-cluster client configuration: %w", err) | ||
} | ||
} else { | ||
cc, err := clientcmd.LoadFromFile(config.KubeConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to load kubeconfig from %s: %w", config.KubeConfig, err) | ||
} | ||
clientConfig, err = clientcmd.NewNonInteractiveClientConfig(*cc, "", &clientcmd.ConfigOverrides{}, nil).ClientConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create client configuration from the provided kubeconfig file: %w", err) | ||
} | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(clientConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create kubernetes clientset: %w", err) | ||
} | ||
|
||
return &Provider{ | ||
clientset: clientset, | ||
config: config, | ||
}, nil | ||
} | ||
|
||
func (p *Provider) Kind() models.CloudProvider { | ||
return models.Kubernetes | ||
} | ||
|
||
func (p *Provider) DiscoverAssets(ctx context.Context) ([]models.AssetType, error) { | ||
return nil, fmt.Errorf("not implemented") | ||
} | ||
|
||
func (p *Provider) RunAssetScan(context.Context, *provider.ScanJobConfig) error { | ||
return fmt.Errorf("not implemented") | ||
} | ||
|
||
func (p *Provider) RemoveAssetScan(context.Context, *provider.ScanJobConfig) error { | ||
return fmt.Errorf("not implemented") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.