autodesk-cloud / ochopod

Your friendly orchestration overlay over Mesos, K8S and more !
http://autodesk-cloud.github.io/ochopod/
Apache License 2.0
122 stars 20 forks source link

Example for using dependency information in configure #10

Open ghost opened 9 years ago

ghost commented 9 years ago

Can you post an example for how to use the dependency information from the cluster in the configure(self, cluster) method?

I know I can do something like this.

def configure(self, cluster):
    pod = cluster.pods[cluster.key]
    return "java -jar $ARTIFACT_TARGET_PATH", { 'SEED_NODE': pod['ip'] }

Does the dependencies dict work the same way?

def configure(self, cluster):
    contactPoints = []
    for k in cluster.dependencies:
        if k == 'cassandra':
            contactPoints.append(cluster.dependencies[k]['ip'])
    contactPointsStr = "[" + ",".join(contactPoints) + "]"
    return "java -jar $ARTIFACT_TARGET_PATH", { 'CONTACT_POINTS': contactPointsStr }
opaugam commented 9 years ago

I see you are doing some real ochopoding there :)

The dependencies are simply defined in the pod script's model. It's an array of identifiers. Each identifier maps to a cluster assumed to be running in the same namespace. Let's take an example (my internal Kafka image I use for my stuff at Autodesk) ->

class Model(Reactive):
    damper = 15.0
    grace = 300.0
    probe_every = 30.0
    sequential = True
    depends_on = ['zookeeper']

That's it. If you run the Kafka container in namespace 'foo', it will only ever configure when it can find at least one container from namespace 'foo' called 'zookeeper'.

You can use the grep() helper on that Cluster wrapper to get you a connection string for a given port. So for instance if I was to do grep("zookeeper", 2181) in my example, i would get a bunch of IP: pointing to my zookeeper containers.

look at https://github.com/autodesk-cloud/ochopod/blob/master/sdk/ochopod/models/piped.py line 50 to see how I use the dependencies dict.

opaugam commented 9 years ago

another example .. this is my HAProxy pod script. In my case HAProxy is configuring itself against a cluster of Play! framework REST endpoint called "api" (which listens on TCP 9000).

class Model(Reactive):

    depends_on = ['api']

class Strategy(Piped):

    cwd = '/opt/haproxy'

    def can_configure(self, cluster):

        #
        # - we need at least one downstream url to redirect traffic to
        #
        assert cluster.grep('api', 9000), 'need 1+ downstream listener'

    def configure(self, cluster):

        #
        # - grep our listeners
        # - render into our 'local' backend directive (which is a standalone file)
        #
        urls = cluster.grep(api,9000).split(',')
        env = Environment(loader=FileSystemLoader(join(dirname(__file__), 'templates')))
        logger.info('%d downstream urls ->\n - %s' % (len(urls), '\n - '.join(urls)))
        mappings = \
            {
                'listeners': {'listener-%d' % index: endpoint for index, endpoint in enumerate(urls)}
            }
ghost commented 9 years ago

Looks great. I'll give it a try.