Skip to content

Commit

Permalink
allow resources to be created for different api groups (crds)
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jul 1, 2021
1 parent aa78857 commit e5458fc
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 60 deletions.
2 changes: 1 addition & 1 deletion helm/kubetruth/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ rules:
verbs: ["get", "list", "create"]
- apiGroups: ["kubetruth.cloudtruth.com"]
resources: ["projectmappings"]
verbs: ["get", "list", "watch"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
{{- end -}}
3 changes: 2 additions & 1 deletion lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def kube_apply(parsed_yml)
kind = parsed_yml["kind"]
name = parsed_yml["metadata"]["name"]
namespace = parsed_yml["metadata"]["namespace"]
apiVersion = parsed_yml["apiVersion"]
if namespace.blank?
namespace = parsed_yml["metadata"]["namespace"] = kubeapi.namespace
end
Expand All @@ -190,7 +191,7 @@ def kube_apply(parsed_yml)
kubeapi.ensure_namespace(namespace) unless @dry_run

begin
resource = kubeapi.get_resource(kind.downcase.pluralize, name, namespace)
resource = kubeapi.get_resource(kind.downcase.pluralize, name, namespace: namespace, apiVersion: apiVersion)
if ! kubeapi.under_management?(resource)
logger.warn "Skipping #{ident} as it doesn't have a label indicating it is under kubetruth management"
else
Expand Down
47 changes: 29 additions & 18 deletions lib/kubetruth/kubeapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,38 @@ def initialize(namespace: nil, token: nil, api_url: nil)
end

@api_url = api_url || 'https://kubernetes.default.svc'
@crd_api_url = "#{@api_url}/apis/kubetruth.cloudtruth.com"
@api_clients = {}
end

def client
@client ||= Kubeclient::Client.new(
@api_url,
'v1',
auth_options: @auth_options,
ssl_options: @ssl_options
)
def api_url(api)
api.present? ? "#{@api_url}/apis/#{api}" : @api_url
end

def crdclient
@crdclient ||= Kubeclient::Client.new(
@crd_api_url,
'v1',
def api_client(api: nil, version: nil)
key = {api: api_url(api), version: version.blank? ? "v1" : version}
@api_clients[key] ||= Kubeclient::Client.new(
key[:api],
key[:version],
auth_options: @auth_options,
ssl_options: @ssl_options
)
end

def client
api_client(api: nil, version: "v1")
end

def crd_client
api_client(api: "kubetruth.cloudtruth.com", version: "v1")
end

def apiVersion_client(apiVersion)
apiVersion ||= "v1"
api_details = apiVersion.split("/")
api_details.insert(0, nil) if api_details.size == 1
api_client(api: api_details[0], version: api_details[1])
end

def ensure_namespace(ns = namespace)
begin
client.get_namespace(ns)
Expand All @@ -73,18 +84,18 @@ def set_managed(resource)
resource["metadata"]["labels"][MANAGED_LABEL_KEY] = MANAGED_LABEL_VALUE
end

def get_resource(resource_name, name, namespace=nil)
client.get_entity(resource_name, name, namespace || self.namespace)
def get_resource(resource_name, name, namespace: nil, apiVersion: nil)
apiVersion_client(apiVersion).get_entity(resource_name, name, namespace || self.namespace)
end

def apply_resource(resource)
resource = Kubeclient::Resource.new(resource) if resource.is_a? Hash
resource_name = resource.kind.downcase.pluralize
client.apply_entity(resource_name, resource, field_manager: "kubetruth")
apiVersion_client(resource.apiVersion).apply_entity(resource_name, resource, field_manager: "kubetruth")
end

def get_project_mappings
mappings = crdclient.get_project_mappings
mappings = crd_client.get_project_mappings
grouped_mappings = {}
mappings.each do |r|
ns = r.metadata.namespace
Expand All @@ -98,9 +109,9 @@ def get_project_mappings
end

def watch_project_mappings(&block)
existing = crdclient.get_project_mappings
existing = crd_client.get_project_mappings
collection_version = existing.resourceVersion
crdclient.watch_project_mappings(resource_version: collection_version, &block)
crd_client.watch_project_mappings(resource_version: collection_version, &block)
end

end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e5458fc

Please sign in to comment.