Giter Club home page Giter Club logo

k8s-objectmatcher's Introduction

license

Kubernetes object matcher

K8S-ObjectMatcher is a Golang library which helps to match Kubernetes objects.

Motivation

Here at Banzai Cloud we love and write lots of Kubernetes operators. While writing some complex operators as the Istio , Vault or Kafka operator, we encountered a huge amount of unnecessary Kubernetes object updates. Most of the operators out there are using reflect.DeepEquals to match the given object's Spec. Unfortunately, this solution is not perfect because every Kubernetes object is amended with different default values while submitted. This library aims to provide finer object matching capabilities to avoid unnecessary updates and more observability on the client side.

Legacy version deprecation notice

There is a legacy version of the lib, that is now deprecated and documented here: docs/legacy.md

How does it work?

The library uses the same method that kubectl apply does under the hood to calculate a patch using the three way merge method. However for this to work properly we need to keep track of the last applied version of our object, let's call it the original. Unfortunately Kubernetes does not keep track of our previously submitted object versions, but we can put it into an annotation like kubectl apply does. Next time we query the current state of the object from the API Server we can extract the original version from the annotation.

Once we have the the original, the current and our new modified object in place the library will take care of the rest.

Example steps demonstrated on a v1.Service object

Create a new object, annotate it, then submit normally

original := &v1.Service{
  ...
}

if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(original); err != nil {
  ...
}

client.CoreV1().Services(original.GetNamespace()).Create(original)

Next time we check the diff and set the last applied annotation in case we have to update

modified := &v1.Service{
  ...
}

current, err := client.CoreV1().Services(modified.GetNamespace()).Get(modified.GetName(), metav1.Getoptions{})

patchResult, err := patch.DefaultPatchMaker.Calculate(current, modified)
if err != nil {
  return err
}

if !patchResult.IsEmpty() {
  if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(modified); err != nil {
  	...
  }
  client.CoreV1().Services(modified.GetNamespace()).Update(modified)
}

Contributing

If you find this project useful here's how you can help:

  • Send a pull request with your new features and bug fixes
  • Help new users with issues they may encounter
  • Support the development of this project and star this repo!

License

Copyright (c) 2017-2019 Banzai Cloud, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

k8s-objectmatcher's People

Contributors

pepov avatar sagikazarmark avatar baluchicken avatar matyix avatar bonifaido avatar waynz0r avatar bweston92 avatar laci21 avatar eachirei 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.