-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cluster list fetchers and cluster resource fetcher
- Loading branch information
Showing
7 changed files
with
474 additions
and
2 deletions.
There are no files selected for viewing
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,58 @@ | ||
# -*- mode:python; coding:utf-8 -*- | ||
# Copyright (c) 2020 IBM Corp. 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. | ||
"""Common error classes.""" | ||
|
||
|
||
class CommandExecutionError(RuntimeError): | ||
"""Represents error at executing command.""" | ||
|
||
def __init__(self, cmd, stdout, stderr, returncode): | ||
"""Initialize an instance. | ||
Initialize an instance with the return values of the command. | ||
""" | ||
self.__cmd = cmd | ||
self.__stdout = stdout | ||
self.__stderr = stderr | ||
self.__returncode = returncode | ||
|
||
def __str__(self): | ||
"""Get information about the command line and its result.""" | ||
return ( | ||
f'Error running command: {self.cmd}\n' | ||
f'returncode: {self.returncode}\n' | ||
f'stdout: {self.stdout}\n' | ||
f'stderr: {self.stderr}' | ||
) | ||
|
||
@property | ||
def cmd(self): | ||
"""Get command line text.""" | ||
return self.__cmd | ||
|
||
@property | ||
def stdout(self): | ||
"""Get standard out text of the command.""" | ||
return self.__stdout | ||
|
||
@property | ||
def stderr(self): | ||
"""Get standard error text of the command.""" | ||
return self.__stderr | ||
|
||
@property | ||
def returncode(self): | ||
"""Get return code of the command.""" | ||
return self.__returncode |
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,70 @@ | ||
# -*- mode:python; coding:utf-8 -*- | ||
# Copyright (c) 2020 IBM Corp. 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. | ||
"""IKS cluster list fetcher.""" | ||
|
||
import json | ||
|
||
from arboretum.common.errors import CommandExecutionError | ||
from arboretum.common.utils import run_command | ||
|
||
from compliance.evidence import store_raw_evidence | ||
from compliance.fetch import ComplianceFetcher | ||
|
||
|
||
class ClusterListFetcher(ComplianceFetcher): | ||
"""Fetch the list of IBM Cloud clusters.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Initialize the fetcher object with configuration settings.""" | ||
cls.logger = cls.locker.logger.getChild( | ||
'ibm_cloud.cluster_list_fetcher' | ||
) | ||
return cls | ||
|
||
@store_raw_evidence('ibm_cloud/cluster_list.json') | ||
def fetch_cluster_list(self): | ||
"""Fetch IBM Cloud cluster list.""" | ||
for account in self.config.get( | ||
'org.ibm_cloud.cluster_list.config.account'): | ||
return json.dumps({account: self._get_cluster_list(account)}) | ||
|
||
def _get_cluster_list(self, account): | ||
|
||
# get credential for the account | ||
api_key = getattr(self.config.creds['ibm_cloud'], account + '_api_key') | ||
|
||
# login | ||
run_command( | ||
f'ibmcloud login --no-region --apikey {api_key}', | ||
secrets=[api_key] | ||
) | ||
|
||
# get cluster list | ||
cluster_list = None | ||
cmd = 'ibmcloud cs cluster ls --json' | ||
try: | ||
cluster_list = run_command(cmd) | ||
except CommandExecutionError as e: | ||
if e.returncode == 2: # "2" means no plugin error | ||
self.logger.warning( | ||
'Failed to execute "ibmcloud cs" command' | ||
' - trying to install the cs plugin' | ||
) | ||
run_command('ibmcloud plugin install kubernetes-service') | ||
cluster_list = run_command(cmd) | ||
else: | ||
raise e | ||
return json.loads(cluster_list) |
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,37 @@ | ||
# -*- mode:python; coding:utf-8 -*- | ||
# Copyright (c) 2020 IBM Corp. 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. | ||
"""Fetch cluster list by using other providers' fetch_cluster_list function.""" | ||
import json | ||
|
||
from compliance.evidence import store_raw_evidence | ||
from compliance.fetch import ComplianceFetcher | ||
|
||
|
||
class ClusterListFetcher(ComplianceFetcher): | ||
"""Fetch BOM (Bill of Materials) as cluster list.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Initialize the fetcher object with configuration settings.""" | ||
cls.logger = cls.locker.logger.getChild( | ||
'kubernetes.cluster_list_fetcher' | ||
) | ||
return cls | ||
|
||
@store_raw_evidence('kubernetes/cluster_list.json') | ||
def fetch_cluster_list(self): | ||
"""Fetch BOM (Bill of Materials) as cluster list.""" | ||
bom = self.config.get('org.kubernetes.cluster_list.config.bom') | ||
return json.dumps(bom) |
Oops, something went wrong.