Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support providing custom Registries #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tmcg-gusto
Copy link

closes: #215

Inserts custom Registries at the beginning of the list of all registries. Supports the use case(s) outlined in the above issue. Tried using this change in our project and it worked really well for us.

Let me know what you think about this approach. Open to other suggestions.

@yannh
Copy link
Owner

yannh commented Jul 9, 2023

@tmcg-gusto the custom registry will be injected at build time right? I see the appeal but as far as Kubeconform is concerned I'm not sure if I would want to provide that... Do you really this need merged if you're going to need to build a custom build pipeline anyway?

@tmcg-gusto
Copy link
Author

@yannh Thanks for the reply!

We have it set up so that the schemas are all pulled with a go:generate script and then embeded into an embed.FS at build time.

At runtime we then create a registry that uses that embed.FS and pass the registry to kubeconform when we initialize the Validator.

Here's a rough estimate of what it looks like:

import (
  "embed"
  "github.com/yannh/kubeconform/pkg/registry"
  "github.com/yannh/kubeconform/pkg/validator"
}
//go:generate fetch_schema.sh
//go:embed schema/*
var k8sSchemaFS embed.FS


// Our custom registry struct
type EmbededRegistry struct {
	fs           fs.FS
	pathTemplate string
	strict       bool
}

// Implementation of the primary logic for the registry fetching from the embedded FS.
func (r EmbededRegistry) DownloadSchema(...) {
  ...   
  r.fs.Open(...)
  ... 
}

func main(){
  registries := []registry.Registry{EmbeddedRegistry {
     fs: k8sSchemaFS,
     pathTemplate: ...,
     strict: false
  }}
  validate, err := validator.New(nil, validator.Opts{Strict: true, Registries: registries, KubernetesVersion: "master"})
  for i, res := range validate.Validate(someFileName, fileHandle){
     // Handle results.
  }
}

Without the change in this PR, we would need to dump the schemas to disk before kubeconform reads them back after specifying the file location. This change allows us to skip that step and read directly from memory. We'll need to provide regular updates to our tool to keep the schemas up to date, but we already planned on making frequent releases.

The only thing kubeconform would be providing is access to customizing how the schemas are loaded and where they are loaded from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support providing preloaded schemas
2 participants