Skip to content

Commit

Permalink
add trim crds descprition configration option (#78)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiwei Yin <zyin@redhat.com>
  • Loading branch information
zhiweiyin318 committed Feb 28, 2022
1 parent 3a1dfe8 commit 9f09a4f
Show file tree
Hide file tree
Showing 5 changed files with 486 additions and 27 deletions.
14 changes: 12 additions & 2 deletions pkg/addonfactory/addonfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type AgentAddonFactory struct {
dir string
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
// trimCRDDescription flag is used to trim the description of CRDs in manifestWork. disabled by default.
trimCRDDescription bool
}

// NewAgentAddonFactory builds an addonAgentFactory instance with addon name and fs.
Expand All @@ -44,6 +46,7 @@ func NewAgentAddonFactory(addonName string, fs embed.FS, dir string) *AgentAddon
Registration: nil,
InstallStrategy: nil,
},
trimCRDDescription: false,
}
}

Expand Down Expand Up @@ -76,6 +79,12 @@ func (f *AgentAddonFactory) WithAgentRegistrationOption(option *agent.Registrati
return f
}

// WithTrimCRDDescription is to enable trim the description of CRDs in manifestWork.
func (f *AgentAddonFactory) WithTrimCRDDescription() *AgentAddonFactory {
f.trimCRDDescription = true
return f
}

// BuildHelmAgentAddon builds a helm agentAddon instance.
func (f *AgentAddonFactory) BuildHelmAgentAddon() (agent.AgentAddon, error) {
if f.scheme == nil {
Expand All @@ -90,7 +99,8 @@ func (f *AgentAddonFactory) BuildHelmAgentAddon() (agent.AgentAddon, error) {
return nil, err
}
// TODO: validate chart
agentAddon := newHelmAgentAddon(f.scheme, userChart, f.getValuesFuncs, f.agentAddonOptions)

agentAddon := newHelmAgentAddon(f, userChart)

return agentAddon, nil
}
Expand All @@ -113,7 +123,7 @@ func (f *AgentAddonFactory) BuildTemplateAgentAddon() (agent.AgentAddon, error)
_ = apiextensionsv1.AddToScheme(f.scheme)
_ = apiextensionsv1beta1.AddToScheme(f.scheme)

agentAddon := newTemplateAgentAddon(f.scheme, f.getValuesFuncs, f.agentAddonOptions)
agentAddon := newTemplateAgentAddon(f)

for _, file := range templateFiles {
template, err := f.fs.ReadFile(file)
Expand Down
27 changes: 14 additions & 13 deletions pkg/addonfactory/helm_agentaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@ type helmBuiltinValues struct {
}

type HelmAgentAddon struct {
decoder runtime.Decoder
chart *chart.Chart
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
decoder runtime.Decoder
chart *chart.Chart
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
trimCRDDescription bool
}

func newHelmAgentAddon(
scheme *runtime.Scheme,
chart *chart.Chart,
getValuesFuncs []GetValuesFunc,
agentAddonOptions agent.AgentAddonOptions) *HelmAgentAddon {
func newHelmAgentAddon(factory *AgentAddonFactory, chart *chart.Chart) *HelmAgentAddon {
return &HelmAgentAddon{
decoder: serializer.NewCodecFactory(scheme).UniversalDeserializer(),
chart: chart,
getValuesFuncs: getValuesFuncs,
agentAddonOptions: agentAddonOptions,
decoder: serializer.NewCodecFactory(factory.scheme).UniversalDeserializer(),
chart: chart,
getValuesFuncs: factory.getValuesFuncs,
agentAddonOptions: factory.agentAddonOptions,
trimCRDDescription: factory.trimCRDDescription,
}
}

Expand Down Expand Up @@ -98,6 +96,9 @@ func (a *HelmAgentAddon) Manifests(
objects = append(objects, object)
}

if a.trimCRDDescription {
objects = trimCRDDescription(objects)
}
return objects, nil
}

Expand Down
235 changes: 235 additions & 0 deletions pkg/addonfactory/helm_agentaddon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestChartAgentAddon_Manifests(t *testing.T) {
agentAddon, err := NewAgentAddonFactory(c.addonName, chartFS, "testmanifests/chart").
WithGetValuesFuncs(getValues, GetValuesFromAddonAnnotation).
WithScheme(c.scheme).
WithTrimCRDDescription().
BuildHelmAgentAddon()
if err != nil {
t.Errorf("expected no error, got err %v", err)
Expand Down Expand Up @@ -140,13 +141,247 @@ func TestChartAgentAddon_Manifests(t *testing.T) {
if object.Name != "test.cluster.open-cluster-management.io" {
t.Errorf("expected v1 crd test, but got %v", object.Name)
}
if !validateTrimCRDv1(object) {
t.Errorf("the crd is not compredded")
}
case *apiextensionsv1beta1.CustomResourceDefinition:
if object.Name != "clusterclaims.cluster.open-cluster-management.io" {
t.Errorf("expected v1 crd clusterclaims, but got %v", object.Name)
}
if !validateTrimCRDv1beta1(object) {
t.Errorf("the crd is not compredded")
}
}

}
})
}
}

func validateTrimCRDv1(crd *apiextensionsv1.CustomResourceDefinition) bool {
versions := crd.Spec.Versions
for i := range versions {
properties := versions[i].Schema.OpenAPIV3Schema.Properties
for _, p := range properties {
if hasDescriptionV1(&p) {
return false
}
}
}
return true
}

func hasDescriptionV1(p *apiextensionsv1.JSONSchemaProps) bool {
if p == nil {
return false
}

if p.Description != "" {
return true
}

if p.Items != nil {
if hasDescriptionV1(p.Items.Schema) {
return true
}
for _, v := range p.Items.JSONSchemas {
if hasDescriptionV1(&v) {
return true
}
}
}

if len(p.AllOf) != 0 {
for _, v := range p.AllOf {
if hasDescriptionV1(&v) {
return true
}
}
}

if len(p.OneOf) != 0 {
for _, v := range p.OneOf {
if hasDescriptionV1(&v) {
return true
}
}
}

if len(p.AnyOf) != 0 {
for _, v := range p.AnyOf {
if hasDescriptionV1(&v) {
return true
}
}
}

if p.Not != nil {
if hasDescriptionV1(p.Not) {
return true
}
}

if len(p.Properties) != 0 {
for _, v := range p.Properties {
if hasDescriptionV1(&v) {
return true
}
}
}

if len(p.PatternProperties) != 0 {
for _, v := range p.PatternProperties {
if hasDescriptionV1(&v) {
return true
}
}
}

if p.AdditionalProperties != nil {
if hasDescriptionV1(p.AdditionalProperties.Schema) {
return true
}
}

if len(p.Dependencies) != 0 {
for _, v := range p.Dependencies {
if hasDescriptionV1(v.Schema) {
return true
}
}
}

if p.AdditionalItems != nil {
if hasDescriptionV1(p.AdditionalItems.Schema) {
return true
}
}

if len(p.Definitions) != 0 {
for _, v := range p.Definitions {
if hasDescriptionV1(&v) {
return true
}
}
}

if p.ExternalDocs != nil && p.ExternalDocs.Description != "" {
return true
}

return false
}

func validateTrimCRDv1beta1(crd *apiextensionsv1beta1.CustomResourceDefinition) bool {
versions := crd.Spec.Versions
for i := range versions {
properties := versions[i].Schema.OpenAPIV3Schema.Properties
for _, p := range properties {
if hasDescriptionV1beta1(&p) {
return false
}
}
}
return true
}

func hasDescriptionV1beta1(p *apiextensionsv1beta1.JSONSchemaProps) bool {
if p == nil {
return false
}

if p.Description != "" {
return true
}

if p.Items != nil {
if hasDescriptionV1beta1(p.Items.Schema) {
return true
}
for _, v := range p.Items.JSONSchemas {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if len(p.AllOf) != 0 {
for _, v := range p.AllOf {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if len(p.OneOf) != 0 {
for _, v := range p.OneOf {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if len(p.AnyOf) != 0 {
for _, v := range p.AnyOf {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if p.Not != nil {
if hasDescriptionV1beta1(p.Not) {
return true
}
}

if len(p.Properties) != 0 {
for _, v := range p.Properties {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if len(p.PatternProperties) != 0 {
for _, v := range p.PatternProperties {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if p.AdditionalProperties != nil {
if hasDescriptionV1beta1(p.AdditionalProperties.Schema) {
return true
}
}

if len(p.Dependencies) != 0 {
for _, v := range p.Dependencies {
if hasDescriptionV1beta1(v.Schema) {
return true
}
}
}

if p.AdditionalItems != nil {
if hasDescriptionV1beta1(p.AdditionalItems.Schema) {
return true
}
}

if len(p.Definitions) != 0 {
for _, v := range p.Definitions {
if hasDescriptionV1beta1(&v) {
return true
}
}
}

if p.ExternalDocs != nil && p.ExternalDocs.Description != "" {
return true
}

return false
}
26 changes: 14 additions & 12 deletions pkg/addonfactory/template_agentaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@ type templateFile struct {
}

type TemplateAgentAddon struct {
decoder runtime.Decoder
templateFiles []templateFile
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
decoder runtime.Decoder
templateFiles []templateFile
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
trimCRDDescription bool
}

func newTemplateAgentAddon(
scheme *runtime.Scheme,
getValuesFuncs []GetValuesFunc,
agentAddonOptions agent.AgentAddonOptions) *TemplateAgentAddon {
func newTemplateAgentAddon(factory *AgentAddonFactory) *TemplateAgentAddon {
return &TemplateAgentAddon{
decoder: serializer.NewCodecFactory(scheme).UniversalDeserializer(),

getValuesFuncs: getValuesFuncs,
agentAddonOptions: agentAddonOptions}
decoder: serializer.NewCodecFactory(factory.scheme).UniversalDeserializer(),
getValuesFuncs: factory.getValuesFuncs,
agentAddonOptions: factory.agentAddonOptions,
trimCRDDescription: factory.trimCRDDescription,
}
}

func (a *TemplateAgentAddon) Manifests(
Expand Down Expand Up @@ -70,6 +69,9 @@ func (a *TemplateAgentAddon) Manifests(
objects = append(objects, object)
}

if a.trimCRDDescription {
objects = trimCRDDescription(objects)
}
return objects, nil
}

Expand Down
Loading

0 comments on commit 9f09a4f

Please sign in to comment.