github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/prow.bzl (about)

     1  # Copyright 2018 The Kubernetes Authors.
     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  load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")
    16  load("@io_bazel_rules_k8s//k8s:objects.bzl", "k8s_objects")
    17  load(
    18      "//:image.bzl",
    19      docker_tags = "tags",
    20  )
    21  
    22  MULTI_KIND = None
    23  
    24  CORE_CLUSTER = "{STABLE_PROW_CLUSTER}"  # For components like hook
    25  
    26  BUILD_CLUSTER = "{STABLE_BUILD_CLUSTER}"  # For untrusted test code
    27  
    28  # image returns the image prefix for the command.
    29  #
    30  # Concretely, image("foo") returns "{STABLE_PROW_REPO}/foo"
    31  # which usually becomes gcr.io/k8s-prow/foo
    32  # (See hack/print-workspace-status.sh)
    33  def prefix(cmd):
    34    return "{STABLE_PROW_REPO}/%s" % cmd
    35  
    36  # target returns the image target for the command.
    37  #
    38  # Concretely, target("foo") returns "//prow/cmd/foo:image"
    39  def target(cmd):
    40    return "//prow/cmd/%s:image" % cmd
    41  
    42  # tags returns a {image: target} map for each cmd.
    43  #
    44  # In particular it will prefix the cmd image name with {STABLE_PROW_REPO}
    45  # Each image gets three tags: {DOCKER_TAG}, latest, latest-{BUILD_USER}
    46  #
    47  # Concretely, tags("hook", "plank") will output the following:
    48  #   {
    49  #     "gcr.io/k8s-prow/hook:20180203-deadbeef": "//prow/cmd/hook:image",
    50  #     "gcr.io/k8s-prow/hook:latest": "//prow/cmd/hook:image",
    51  #     "gcr.io/k8s-prow/hook:latest-fejta": "//prow/cmd/hook:image",
    52  #     "gcr.io/k8s-prow/plank:20180203-deadbeef": "//prow/cmd/plank:image",
    53  #     "gcr.io/k8s-prow/plank:latest": "//prow/cmd/plank:image",
    54  #     "gcr.io/k8s-prow/plank:latest-fejta": "//prow/cmd/plank:image",
    55  #   }
    56  def tags(*cmds):
    57    # Create :YYYYmmdd-commitish :latest :latest-USER tags
    58    return docker_tags(**{prefix(cmd): target(cmd) for cmd in cmds})
    59  
    60  def object(name, cluster=CORE_CLUSTER, **kwargs):
    61    k8s_object(
    62        name = name,
    63        cluster = cluster,
    64        **kwargs
    65    )
    66  
    67  # component generates k8s_object rules and returns a {kind: [targets]} map.
    68  #
    69  # This will generate a k8s_object rule for each specified kind.
    70  # Use MULTI_KIND for a multi-document yaml (this returns nothing).
    71  # Assumes files exist at <cmd>_<kind>.yaml
    72  #
    73  # Concretely, component("hook", "service", "deployment") will create the following:
    74  #   object("hook_service", kind="service", template=":hook_service.yaml")
    75  #   object("hook_deployment", kind="deployment", template=":hook_deployment.yaml")
    76  # And return the following:
    77  #   {
    78  #     "hook": [":hook_service", ":hook_deployment",
    79  #     "service": [":hook_service"],
    80  #     "deployment": [":hook_deployment"],
    81  #   }
    82  def component(cmd, *kinds, **kwargs):
    83    targets = {}
    84    for k in kinds:
    85        if k == MULTI_KIND:
    86          n = cmd
    87        else:
    88          n = "%s_%s" % (cmd, k)
    89        kwargs["name"] = n
    90        kwargs["kind"] = k
    91        kwargs["template"] = ":%s.yaml" % n
    92        object(**kwargs)
    93        if k != MULTI_KIND:
    94          targets.setdefault(cmd,[]).append(":%s" % n)
    95          targets.setdefault(k,[]).append(":%s" % n)
    96    return targets
    97  
    98  # release packages multiple components into a release.
    99  #
   100  # Generates a k8s_objects() rule for each component and kind, as well as an
   101  # target which includes everything.
   102  #
   103  # Thus you can do things like:
   104  #   bazel run //prow/cluster:hook.apply  # Update all hook resources
   105  #   bazel run //prow/cluster:deployment.apply  # Update all deployments in prow
   106  #
   107  # Concretely, the following:
   108  #   release(
   109  #     "fancy",
   110  #     component("hook", "deployment", "service"),
   111  #     compoennt("plank", "deployment"),
   112  #   )
   113  # Generates the five following rules:
   114  #   k8s_objects(name = "hook", objects=[":hook_deployment", ":hook_service"])
   115  #   k8s_objects(name = "plank", objects=[":plank_deployment"])
   116  #   k8s_objects(name = "deployment", objects=[":hook_deployment", ":plank_deployment"])
   117  #   k8s_objects(name = "service", objects=[":hook_service"])
   118  #   k8s_objects(name = "fancy", objects=[":hook", ":plank", ":deployment", ":service"])
   119  def release(name, *components):
   120    targets = {}
   121    objs = []
   122    for cs in components:
   123      for (n, ts) in cs.items():
   124        targets.setdefault(n, []).extend(ts)
   125    for (piece, ts) in targets.items():
   126      k8s_objects(
   127          name = piece,
   128          objects = ts,
   129      )
   130      objs.append(":%s" % piece)
   131    k8s_objects(
   132        name = name,
   133        objects=objs,
   134    )