Giter Site home page Giter Site logo

k8s-utility-client's Introduction

K8s Utility Client

A library creating a set of Kubernetes clients - both standard and dynamic that can be used within a K8s cluster, or without.

The Operator Framework makes a k8s client that is insufficient to generating a Dynamic client, so I put this together to make all the clients I need.

It works the same whether it's running inside a cluster or without. I don't have to care.

Sometimes you just want things to just work.

Current Release

Go Report Card

Go Doc

Usage

Creating a Client

Creating a clientis easy. Just use:

    client, err := NewK8sClients()
    if err != nil {
        log.Fatalf("failed creating client: %s", err)
    }

It will look for your config file in ~/.kube/config. The dynamic client must be able to reach a k8s cluster in order to do it's thing.

Loading Resource Files

To load the files, run

    interfaces, objects, err := client.ResourcesAndObjectsFromFile(fileName)
    if err != nil {
        log.Fatalf("failed to load yaml file %s: %s", fileName, err)
    }

Applying Resources

The ApplyResources() method is smart enough to Create or Update, depending on whether the resources being applied already exist or not.

    ctx := context.TODO()

    fmt.Printf("Creating resources in k8s.\n")
    err = client.ApplyResources(ctx, interfaces, objects)
    if err != nil {
        t.Errorf("failed to apply resources: %s", err)
    }

Getting Resources

To Get and examine resources, use the 'objects' and 'interfaces' returned by loading:

    fmt.Printf("Verifying resources in k8s.\n")
    for i, obj := range objects {
        ri := interfaces[i]

        o, err := ri.Get(ctx, obj.GetName(), metav1.GetOptions{})
        if err != nil {
            t.Errorf("failed getting resource %s kind %s", obj.GetName(), obj.GetKind())
        }

        assert.Equal(t, obj.GetName(), o.GetName(), "Created Resource name doesn't match expectation.")
        assert.Equal(t, obj.GetKind(), o.GetKind(), "Created Resource Kind does not match expectations.")
        assert.Equal(t, obj.GetNamespace(), o.GetNamespace(), "Created Resource Namespace does not match expectations.")

    }

Deleting Resources

To clean up, call DeleteResources()

    ctx := context.TODO()

    fmt.Printf("Cleaning up resources in k8s.\n")
    err = client.DeleteResources(ctx, interfaces, objects)
    if err != nil {
        t.Errorf("failed deleting resources: %s", err)
    }

k8s-utility-client's People

Contributors

nikogura avatar

Watchers

 avatar

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.