forked from carvel-dev/kapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add change-group placeholders for granular ordering of resources (car…
…vel-dev#310) * Update default change groups and rules for granular ordering(POC) * Wrote PlaceholderParser which can be instantiated with a resource to parse changeGroup names * Making PlaceholderParser more specific as ChangeGroupName. Resolving nits * Optimisation: Using regex based string replace instead of looping over placeholders * Add change graph test for change group placeholders * Add more resources to TestChangeGraphWithNamespaceAndCRDs * Add back the namespace matcher Co-authored-by: Soumik Majumder <soumikm@vmware.com>
- Loading branch information
1 parent
e812483
commit 5ce3839
Showing
5 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package diffgraph | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
ctlres "github.com/k14s/kapp/pkg/kapp/resources" | ||
ctlcrd "github.com/k14s/kapp/pkg/kapp/resourcesmisc" | ||
) | ||
|
||
type ChangeGroupName struct { | ||
name string | ||
resource ctlres.Resource | ||
} | ||
|
||
func NewChangeGroupNameForResource(name string, resource ctlres.Resource) ChangeGroupName { | ||
return ChangeGroupName{name, resource} | ||
} | ||
|
||
var ( | ||
placeholderMatcher = regexp.MustCompile("{.+?}") | ||
) | ||
|
||
// Placeholders have the format {placeholder-name} | ||
// Other patterns like ${placeholder-name} are commonly used by other operators/tools | ||
func (c ChangeGroupName) AsString() (string, error) { | ||
var crdKind, crdGroup string | ||
var err error | ||
crd := ctlcrd.NewAPIExtensionsVxCRD(c.resource) | ||
if crd != nil { | ||
crdKind, err = crd.Kind() | ||
if err != nil { | ||
return c.name, err | ||
} | ||
crdGroup, err = crd.Group() | ||
if err != nil { | ||
return c.name, err | ||
} | ||
} | ||
|
||
values := map[string]string{ | ||
"{api-group}": c.resource.APIGroup(), | ||
"{kind}": c.resource.Kind(), | ||
"{name}": c.resource.Name(), | ||
"{namespace}": c.resource.Namespace(), | ||
"{crd-kind}": crdKind, | ||
"{crd-group}": crdGroup, | ||
} | ||
|
||
replaced := placeholderMatcher.ReplaceAllStringFunc(c.name, func(placeholder string) string { | ||
value, found := values[placeholder] | ||
if !found { | ||
err = fmt.Errorf("Expected placeholder to be one of these: %s but was %s", c.placeholders(values), placeholder) | ||
} | ||
if value == "" { | ||
err = fmt.Errorf("Placeholder %s does not have a value for target resource (hint: placeholders with the 'crd-' prefix can only be used with CRDs)", placeholder) | ||
} | ||
return value | ||
}) | ||
|
||
return replaced, err | ||
} | ||
|
||
func (c ChangeGroupName) placeholders(values map[string]string) (placeholders []string) { | ||
for k := range values { | ||
placeholders = append(placeholders, k) | ||
} | ||
return placeholders | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters