Code Monkey home page Code Monkey logo

Comments (2)

nicks avatar nicks commented on June 10, 2024 1

thanks for the report!

from tilt.

rrmistry avatar rrmistry commented on June 10, 2024

Currently working with a non-ideal workaround below:

# Intercept Tilt resource naming and also apply labels to resources
def apply_resource_labels(id):

    # Set the TILT_RESOURCE environment variable
    # This allows us to share information globally between Tiltfile functions scope and within apply_resource_labels function
    resource_identifier = "%s/%s/%s" % (id.namespace, id.kind, id.name)
    if os.getenv("TILT_RESOURCE", None) == None:
        os.environ["TILT_RESOURCE"] = resource_identifier
    else:
        if os.environ["TILT_RESOURCE"].find(resource_identifier) <= -1:
            if os.environ["TILT_RESOURCE"] != "":
                os.environ["TILT_RESOURCE"] += ","
            os.environ["TILT_RESOURCE"] += resource_identifier

    return id.name

# Reference: https://docs.tilt.dev/api#api.workload_to_resource_function
workload_to_resource_function(apply_resource_labels)

if os.getenv("TILT_RESOURCE", None) != None:
    # Will not execute on first run
    # But will execute on subsequent runs (i.e. when Tilt control loop triggers again on file/folder/resource changes)

    for r in os.environ["TILT_RESOURCE"].split(","):

        # Split the resource identifier into namespace, kind and name
        ns = r.split("/")[0]
        kd = r.split("/")[1].lower()
        nm = r.split("/")[2]

        resource_value = str(
            local(
                command="""
                    kubectl --namespace %s get %s %s --output jsonpath='{.metadata.labels.tilt\\.dev\\/resource}' --ignore-not-found=true
                """ % (
                    ns, kd, nm,
                ),
                echo_off=True, # skips printing command to log.
                quiet=True, # skips printing output to log.
            )
        )

        label_value = str(
            local(
                command="""
                    kubectl --namespace %s get %s %s --output jsonpath='{.metadata.labels.tilt\\.dev\\/label}' --ignore-not-found=true
                """ % (
                    ns, kd, nm,
                ),
                echo_off=True, # skips printing command to log.
                quiet=True, # skips printing output to log.
            )
        )

        # # Set sensible default label values
        # if label_value == None or len(label_value) <= 0:
        #     if "prometheus" in resource_value:
        #         label_value = "monitoring"
        #     elif "ingress-nginx" in resource_value:
        #         label_value = "ingress"
        #     # elif ...

        if label_value and len(label_value) > 0:
            if resource_value and len(resource_value) > 0:
                # Reference: https://docs.tilt.dev/api.html#api.k8s_resource
                k8s_resource(
                    workload=nm,
                    new_name=resource_value,
                    labels=[label_value],
                )
            else:
                # Reference: https://docs.tilt.dev/api.html#api.k8s_resource
                k8s_resource(
                    workload=nm,
                    labels=[label_value],
                )

Note: This is not ideal because it will not set resource names / labels on first pass of Tiltfile parsing. On second pass, the TILT_RESOURCE environment variable value will be accessible by rest of Tiltfile functions and so can be used to programmatically access them and detect and set Tilt resource names and labels

from tilt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.