Add configmap/secret hash functionality (#48)

* * * Add configmap/secret hash functionality

Signed-off-by: Alina Buzachis <abuzachis@redhat.com>

* * Add changelog fragment

Signed-off-by: Alina Buzachis <abuzachis@redhat.com>
This commit is contained in:
Alina Buzachis
2021-04-20 14:27:25 +02:00
committed by GitHub
parent cc10268adf
commit edc48ee577
5 changed files with 248 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ from datetime import datetime
from distutils.version import LooseVersion
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_MAP, AUTH_ARG_SPEC)
from ansible_collections.kubernetes.core.plugins.module_utils.hashes import generate_hash
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.six import iteritems, string_types
@@ -60,7 +61,6 @@ except ImportError:
K8S_CONFIG_HASH_IMP_ERR = None
try:
from openshift.helper.hashes import generate_hash
from openshift.dynamic.exceptions import KubernetesValidateMissing
HAS_K8S_CONFIG_HASH = True
except ImportError:

View File

@@ -0,0 +1,67 @@
# Copyright [2017] [Red Hat, Inc.]
#
# 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.
# Implement ConfigMapHash and SecretHash equivalents
# Based on https://github.com/kubernetes/kubernetes/pull/49961
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import hashlib
try:
import string
maketrans = string.maketrans
except AttributeError:
maketrans = str.maketrans
try:
from collections import OrderedDict
except ImportError:
from orderreddict import OrderedDict
def sorted_dict(unsorted_dict):
result = OrderedDict()
for (k, v) in sorted(unsorted_dict.items()):
if isinstance(v, dict):
v = sorted_dict(v)
result[k] = v
return result
def generate_hash(resource):
# Get name from metadata
resource['name'] = resource.get('metadata', {}).get('name', '')
if resource['kind'] == 'ConfigMap':
marshalled = marshal(sorted_dict(resource), ['data', 'kind', 'name'])
del(resource['name'])
return encode(marshalled)
if resource['kind'] == 'Secret':
marshalled = marshal(sorted_dict(resource), ['data', 'kind', 'name', 'type'])
del(resource['name'])
return encode(marshalled)
raise NotImplementedError
def marshal(data, keys):
ordered = OrderedDict()
for key in keys:
ordered[key] = data.get(key, "")
return json.dumps(ordered, separators=(',', ':')).encode('utf-8')
def encode(resource):
return hashlib.sha256(resource).hexdigest()[:10].translate(maketrans("013ae", "ghkmt"))