Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Apr 20, 2022
1 parent 32d9a15 commit 1125eb0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
14 changes: 7 additions & 7 deletions pkg/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func ApplyCluster(ctx context.Context, clusterInfo *config.ClusterInfo, p provid
logger.Infof("Assuming cluster is already provisioned, as %q exists...", kubeconfigPath)
}

logger.Info("Applying workshopctl Namespace")
if _, err := kubectl(ctx, kubeconfigPath).
Create("namespace", "", constants.WorkshopctlNamespace, true, false).
Run(); err != nil {
return err
}

// Setup GitOps sync
if err := gotk.SetupGitOps(ctx, clusterInfo); err != nil {
return err
Expand All @@ -65,13 +72,6 @@ func ApplyCluster(ctx context.Context, clusterInfo *config.ClusterInfo, p provid
return kubectl(ctx, kubeconfigPath).WithNS(constants.WorkshopctlNamespace)
}

logger.Info("Applying workshopctl Namespace")
if _, err := kubectl(ctx, kubeconfigPath).
Create("namespace", "", constants.WorkshopctlNamespace, true, false).
Run(); err != nil {
return err
}

paramFlags := []string{}
// Append secret parameters
parameters := keyval.FromClusterInfo(clusterInfo)
Expand Down
18 changes: 18 additions & 0 deletions pkg/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cloud-native-nordics/workshopctl/pkg/constants"
"github.com/cloud-native-nordics/workshopctl/pkg/util"
log "github.com/sirupsen/logrus"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/yaml"
)

Expand All @@ -40,6 +41,7 @@ func SetupInternalChartCache(ctx context.Context) ([]*ChartData, error) {
if err != nil {
return nil, err
}

// Now that the internal files are extracted to disk,
// process them exactly as normal "external" charts
chartCache := make([]*ChartData, 0, len(charts))
Expand Down Expand Up @@ -169,6 +171,7 @@ func GenerateChart(ctx context.Context, cd *ChartData, clusterInfo *config.Clust
processorChain = append(processorChain, valuesProcessors...)
processorChain = append(processorChain, []Processor{
&helmTemplateProcessor{namespace},
&nsProcessor{namespace},
&unescapeGoTmpls{},
}...)
processorChain = append(processorChain, chartProcessors...)
Expand Down Expand Up @@ -274,3 +277,18 @@ func (pr *unescapeGoTmpls) Process(ctx context.Context, _ *ChartData, _ *keyval.
_, err = w.Write(b)
return err
}

type nsProcessor struct {
ns string
}

func (pr *nsProcessor) Process(ctx context.Context, cd *ChartData, p *keyval.Parameters, r io.Reader, w io.Writer) error {
return util.KYAMLFilter(r, w, util.KYAMLFilterFunc(
func(node *kyaml.RNode) (*kyaml.RNode, error) {
return node, node.PipeE(
kyaml.LookupCreate(kyaml.MappingNode, "metadata"),
kyaml.FieldMatcher{Name: "namespace", Create: kyaml.NewScalarRNode(pr.ns)},
)
},
))
}
8 changes: 4 additions & 4 deletions pkg/gotk/gotk.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func SetupGitOps(ctx context.Context, info *config.ClusterInfo) error {

// Make sure we have all prereqs
kubeConfigArg := "--kubeconfig=" + info.Index.KubeConfigPath()
_, _, err := util.Command(ctx,
/*_, _, err := util.Command(ctx,
"gotk",
kubeConfigArg,
"check",
"--pre",
).Run()
if err != nil {
return err
}
}*/

var provider string
switch info.Git.RepoStruct.Domain {
Expand All @@ -54,8 +54,8 @@ func SetupGitOps(ctx context.Context, info *config.ClusterInfo) error {
// TODO: That doesn't work in current gotk, rework that maybe upstream too?
// This command installs the toolkit into the target cluster, and starts reconciling our
// given cluster directory for changes.
_, _, err = util.Command(ctx,
"gotk",
_, _, err := util.Command(ctx,
"flux",
kubeConfigArg,
"bootstrap",
provider,
Expand Down
18 changes: 18 additions & 0 deletions pkg/provider/digitalocean/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
)

const expectedNSDomains = `ns3.digitalocean.com.
ns1.digitalocean.com.
ns2.digitalocean.com.
`

func NewDigitalOceanDNSProvider(ctx context.Context, p *config.Provider, rootDomain string) (provider.DNSProvider, error) {
return &DigitalOceanDNSProvider{
doCommon: initCommon(ctx, p),
Expand All @@ -41,6 +46,19 @@ func (do *DigitalOceanDNSProvider) ValuesProcessors() []gen.Processor {
func (do *DigitalOceanDNSProvider) EnsureZone(ctx context.Context) error {
logger := util.Logger(ctx)

out, _, err := util.ShellCommand(ctx, "dig +short NS %s", do.rootDomain).
WithDryRunContent(expectedNSDomains).
Run()
if err != nil {
return err
}
for i := 1; i <= 3; i++ {
domain := fmt.Sprintf("ns%d.digitalocean.com.", i)
if !strings.Contains(out, domain) {
return fmt.Errorf("%s doesn't have an NS record to %s", do.rootDomain, domain)
}
}

// First, check if it exists
logger.Debugf("Ensuring domain %s is managed by DigitalOcean DNS", do.rootDomain)
domain, resp, err := do.c.Domains.Get(ctx, do.rootDomain)
Expand Down
16 changes: 11 additions & 5 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,11 @@ func ShellCommand(ctx context.Context, format string, args ...interface{}) *Exec
}

type ExecUtil struct {
cmd *exec.Cmd
outBuf *bytes.Buffer
ctx context.Context
logger *logrus.Entry
cmd *exec.Cmd
outBuf *bytes.Buffer
ctx context.Context
logger *logrus.Entry
dryRunOut string
}

func (e *ExecUtil) Cmd() *exec.Cmd {
Expand Down Expand Up @@ -266,13 +267,18 @@ func (e *ExecUtil) WithEnv(envVars ...string) *ExecUtil {
return e
}

func (e *ExecUtil) WithDryRunContent(out string) *ExecUtil {
e.dryRunOut = out
return e
}

func (e *ExecUtil) Run() (output string, exitCode int, cmdErr error) {
cmdArgs := strings.Join(e.cmd.Args, " ")

// Don't do this if we're dry-running
if IsDryRun(e.ctx) {
e.logger.Infof("Would execute command %q", cmdArgs)
return "", 0, nil
return e.dryRunOut, 0, nil
}

// Always capture stdout output to e.outBuf
Expand Down

0 comments on commit 1125eb0

Please sign in to comment.