Skip to content

Commit

Permalink
Early apply namespaces in addition to CRDs
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed May 15, 2020
1 parent 45d771a commit 777156f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
14 changes: 7 additions & 7 deletions kotsadm/operator/pkg/client/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ func (c *Client) ensureResourcesPresent(applicationManifests ApplicationManifest
return nil, errors.Wrap(err, "failed to decode manifests")
}

customResourceDefinitions, otherDocs, err := splitMutlidocYAMLIntoCRDsAndOthers(decoded)
firstApplyDocs, otherDocs, err := splitMutlidocYAMLIntoFirstApplyAndOthers(decoded)
if err != nil {
return nil, errors.Wrap(err, "failed to split decoded into crds and other")
}

// We don't dry run if there's a crd becasue there's a likely chance that the
// other docs has a custom resource using it
shouldDryRun := customResourceDefinitions == nil
shouldDryRun := firstApplyDocs == nil
if shouldDryRun {
byNamespace, err := docsByNamespace(decoded, targetNamespace)
if err != nil {
Expand Down Expand Up @@ -240,15 +240,15 @@ func (c *Client) ensureResourcesPresent(applicationManifests ApplicationManifest

}

if len(customResourceDefinitions) > 0 {
log.Println("applying custom resource definition(s)")
if len(firstApplyDocs) > 0 {
log.Println("applying first apply docs (CRDs, Namespaces)")

// CRDs don't have namespaces, so we can skip splitting

applyStdout, applyStderr, applyErr := kubernetesApplier.Apply("", applicationManifests.AppSlug, customResourceDefinitions, false, applicationManifests.Wait, applicationManifests.AnnotateSlug)
applyStdout, applyStderr, applyErr := kubernetesApplier.Apply("", applicationManifests.AppSlug, firstApplyDocs, false, applicationManifests.Wait, applicationManifests.AnnotateSlug)
if applyErr != nil {
log.Printf("stdout (apply CRDS) = %s", applyStdout)
log.Printf("stderr (apply CRDS) = %s", applyStderr)
log.Printf("stdout (first apply) = %s", applyStdout)
log.Printf("stderr (first apply) = %s", applyStderr)
log.Printf("error (CRDS): %s", applyErr.Error())

if err := c.sendResult(applicationManifests, applyErr != nil, []byte{}, []byte{}, applyStdout, applyStderr); err != nil {
Expand Down
12 changes: 7 additions & 5 deletions kotsadm/operator/pkg/client/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import (
"gopkg.in/yaml.v2"
)

func splitMutlidocYAMLIntoCRDsAndOthers(multidoc []byte) ([]byte, []byte, error) {
crds := []string{}
func splitMutlidocYAMLIntoFirstApplyAndOthers(multidoc []byte) ([]byte, []byte, error) {
firstApply := []string{}
other := []string{}

docs := strings.Split(string(multidoc), "\n---\n")
for _, doc := range docs {
if IsCRD([]byte(doc)) {
crds = append(crds, doc)
firstApply = append(firstApply, doc)
} else if IsNamespace([]byte(doc)) {
firstApply = append(firstApply, doc)
} else {
other = append(other, doc)
}
}

// if there were no crds, don't return the touched docs, keep the original
if len(crds) == 0 {
if len(firstApply) == 0 {
return nil, multidoc, nil
}

return []byte(strings.Join(crds, "\n---\n")), []byte(strings.Join(other, "\n---\n")), nil
return []byte(strings.Join(firstApply, "\n---\n")), []byte(strings.Join(other, "\n---\n")), nil
}

func docsByNamespace(multidoc []byte, defaultNamespace string) (map[string][]byte, error) {
Expand Down
10 changes: 10 additions & 0 deletions kotsadm/operator/pkg/client/gvkn.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func IsCRD(content []byte) bool {
return o.APIVersion == "apiextensions.k8s.io/v1beta1" && o.Kind == "CustomResourceDefinition"
}

func IsNamespace(content []byte) bool {
o := OverlySimpleGVKWithName{}

if err := yaml.Unmarshal(content, &o); err != nil {
return false
}

return o.APIVersion == "v1" && o.Kind == "Namespace"
}

func findPodsByOwner(name string, namespace string, gvk *k8sschema.GroupVersionKind) ([]*corev1.Pod, error) {
cfg, err := config.GetConfig()
if err != nil {
Expand Down

0 comments on commit 777156f

Please sign in to comment.