...

Text file src/github.com/GoogleCloudPlatform/k8s-config-connector/scripts/samples-should-have-required-project-ref/FindMissingProjectRef.py

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/scripts/samples-should-have-required-project-ref

     1# Copyright 2022 Google LLC
     2#
     3# Licensed under the Apache License, Version 2.0 (the "License");
     4# you may not use this file except in compliance with the License.
     5# You may obtain a copy of the License at
     6#
     7#      http://www.apache.org/licenses/LICENSE-2.0
     8#
     9# Unless required by applicable law or agreed to in writing, software
    10# distributed under the License is distributed on an "AS IS" BASIS,
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12# See the License for the specific language governing permissions and
    13# limitations under the License.
    14
    15# Sample usage: python3 FindMissingProjectRef.py /usr/local/google/home/shuxiancai/go/src/github.com/GoogleCloudPlatform/k8s-config-connector
    16import glob
    17import sys
    18import yaml
    19
    20rootPath = sys.argv[1]
    21kindsWithRequiredProjectRef = set()
    22for filepath in glob.iglob(rootPath + '/config/crds/resources/*.yaml'):
    23  with open(filepath) as f:
    24    dataMap = yaml.safe_load(f)
    25    schemaprops = dataMap["spec"]["versions"][0]["schema"]["openAPIV3Schema"]["properties"]
    26    if "spec" in schemaprops.keys():
    27      spec = dataMap["spec"]["versions"][0]["schema"]["openAPIV3Schema"]["properties"]["spec"]
    28      properties = spec["properties"]
    29      if "projectRef" in properties.keys() and "required" in spec.keys():
    30        required = spec["required"]
    31        if "projectRef" in required:
    32          kindsWithRequiredProjectRef.add(dataMap["spec"]["names"]["kind"])
    33
    34for filepath in glob.glob(rootPath + '/config/samples/resources/**/*.yaml', recursive=True):
    35  with open(filepath) as f:
    36    dataMaps = yaml.safe_load_all(f)
    37    for dataMap in dataMaps:
    38      kind = dataMap["kind"]
    39      if kind in kindsWithRequiredProjectRef and "spec" in dataMap.keys() and "projectRef" not in dataMap["spec"]:
    40        print(filepath)

View as plain text