Skip to content

Commit

Permalink
lint hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-mccoy committed Jan 1, 2023
1 parent abf6a56 commit 0780bb7
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 107 deletions.
1 change: 1 addition & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var rootCmd = &cobra.Command{
},
}

// Execute is the entrypoint for the CLI
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func GetArch(archs ...string) string {
return runtime.GOARCH
}

// Timestamp of when the CLI was started
// GetStartTime Timestamp of when the CLI was started
func GetStartTime() int64 {
return operationStartTime
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/api/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func RequireSecret(validToken string) func(http.Handler) http.Handler {
}

// Connect is a head-only request to test the connection
func Connect(w http.ResponseWriter, r *http.Request) {
func Connect(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
8 changes: 4 additions & 4 deletions src/internal/api/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/defenseunicorns/zarf/src/types"
)

// Read the zarf state secret from the cluster, if it exists.
func ReadState(w http.ResponseWriter, r *http.Request) {
// ReadState reads the zarf state secret from the cluster, if it exists.
func ReadState(w http.ResponseWriter, _ *http.Request) {
message.Debug("state.Read()")

data, err := cluster.NewClusterOrDie().LoadZarfState()
Expand All @@ -30,8 +30,8 @@ func ReadState(w http.ResponseWriter, r *http.Request) {
}
}

// Update the zarf state secret in the cluster.
func UpdateState(w http.ResponseWriter, r *http.Request) {
// UpdateState updates the zarf state secret in the cluster.
func UpdateState(w http.ResponseWriter, _ *http.Request) {
message.Debug("state.Update()")

var data types.ZarfState
Expand Down
4 changes: 2 additions & 2 deletions src/internal/api/components/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/defenseunicorns/zarf/src/internal/api/common"
)

// ListDeployedPackages writes a list of packages that have been deployed to the connected cluster.
func ListDeployingComponents(w http.ResponseWriter, r *http.Request) {
// ListDeployingComponents writes a list of packages that have been deployed to the connected cluster.
func ListDeployingComponents(w http.ResponseWriter, _ *http.Request) {
deployingPackages := config.GetDeployingComponents()
common.WriteJSONResponse(w, deployingPackages, http.StatusOK)
}
1 change: 1 addition & 0 deletions src/internal/cluster/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/message"
)

// Cluster is a wrapper for the k8s package that provides zarf-specific cluster management functions
type Cluster struct {
Kube *k8s.K8s
}
Expand Down
17 changes: 16 additions & 1 deletion src/internal/cluster/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
)

// Wait for the target pod(s) to come up and inject the data into them
// HandleDataInjection waits for the target pod(s) to come up and inject the data into them
// todo: this currently requires kubectl but we should have enough k8s work to make this native now
func (c *Cluster) HandleDataInjection(wg *sync.WaitGroup, data types.ZarfDataInjection, componentPath types.ComponentPaths) {
message.Debugf("packager.handleDataInjections(%#v, %#v, %#v)", wg, data, componentPath)
Expand Down Expand Up @@ -102,6 +102,21 @@ iterator:
continue iterator
}
}

// Leave a marker in the target container for pods to track the sync action
cpPodExec = fmt.Sprintf("%s -C %s %s | %s -- %s",
tarExec,
componentPath.DataInjections,
config.GetDataInjectionMarker(),
kubectlExec,
untarExec,
)

if err := exec.CmdWithPrint("sh", "-c", cpPodExec); err != nil {
message.Warnf("Error saving the zarf sync completion file after injection into pod %#v\n", pod)
continue iterator
}

}

// Do not look for a specific container after injection in case they are running an init container
Expand Down
82 changes: 41 additions & 41 deletions src/internal/packager/git/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func (g *Git) CreateReadOnlyUser() error {
tunnel.Connect(cluster.ZarfGit, false)
defer tunnel.Close()

tunnelUrl := tunnel.Endpoint()
tunnelURL := tunnel.Endpoint()

// Determine if the read only user already exists
getUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users", tunnelUrl)
getUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users", tunnelURL)
getUserRequest, _ := netHttp.NewRequest("GET", getUserEndpoint, nil)
out, err := g.DoHttpThings(getUserRequest, g.Server.PushUsername, g.Server.PushPassword)
out, err := g.DoHTTPThings(getUserRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("GET %s:\n%s", getUserEndpoint, string(out))
if err != nil {
return err
Expand All @@ -60,49 +60,49 @@ func (g *Git) CreateReadOnlyUser() error {
"password": g.Server.PullPassword,
}
updateUserData, _ := json.Marshal(updateUserBody)
updateUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users/%s", tunnelUrl, g.Server.PullUsername)
updateUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users/%s", tunnelURL, g.Server.PullUsername)
updateUserRequest, _ := netHttp.NewRequest("PATCH", updateUserEndpoint, bytes.NewBuffer(updateUserData))
out, err = g.DoHttpThings(updateUserRequest, g.Server.PushUsername, g.Server.PushPassword)
out, err = g.DoHTTPThings(updateUserRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("PATCH %s:\n%s", updateUserEndpoint, string(out))
return err
} else {
// Create json representation of the create-user request body
createUserBody := map[string]interface{}{
"username": g.Server.PullUsername,
"password": g.Server.PullPassword,
"email": "[email protected]",
"must_change_password": false,
}
createUserData, err := json.Marshal(createUserBody)
if err != nil {
return err
}
}

// Send API request to create the user
createUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users", tunnelUrl)
createUserRequest, _ := netHttp.NewRequest("POST", createUserEndpoint, bytes.NewBuffer(createUserData))
out, err = g.DoHttpThings(createUserRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("POST %s:\n%s", createUserEndpoint, string(out))
if err != nil {
return err
}
// Create json representation of the create-user request body
createUserBody := map[string]interface{}{
"username": g.Server.PullUsername,
"password": g.Server.PullPassword,
"email": "[email protected]",
"must_change_password": false,
}
createUserData, err := json.Marshal(createUserBody)
if err != nil {
return err
}

// Make sure the user can't create their own repos or orgs
updateUserBody := map[string]interface{}{
"login_name": g.Server.PullUsername,
"max_repo_creation": 0,
"allow_create_organization": false,
}
updateUserData, _ := json.Marshal(updateUserBody)
updateUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users/%s", tunnelUrl, g.Server.PullUsername)
updateUserRequest, _ := netHttp.NewRequest("PATCH", updateUserEndpoint, bytes.NewBuffer(updateUserData))
out, err = g.DoHttpThings(updateUserRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("PATCH %s:\n%s", updateUserEndpoint, string(out))
// Send API request to create the user
createUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users", tunnelURL)
createUserRequest, _ := netHttp.NewRequest("POST", createUserEndpoint, bytes.NewBuffer(createUserData))
out, err = g.DoHTTPThings(createUserRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("POST %s:\n%s", createUserEndpoint, string(out))
if err != nil {
return err
}

// Make sure the user can't create their own repos or orgs
updateUserBody := map[string]interface{}{
"login_name": g.Server.PullUsername,
"max_repo_creation": 0,
"allow_create_organization": false,
}
updateUserData, _ := json.Marshal(updateUserBody)
updateUserEndpoint := fmt.Sprintf("http://%s/api/v1/admin/users/%s", tunnelURL, g.Server.PullUsername)
updateUserRequest, _ := netHttp.NewRequest("PATCH", updateUserEndpoint, bytes.NewBuffer(updateUserData))
out, err = g.DoHTTPThings(updateUserRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("PATCH %s:\n%s", updateUserEndpoint, string(out))
return err
}

func (g *Git) addReadOnlyUserToRepo(tunnelUrl, repo string) error {
func (g *Git) addReadOnlyUserToRepo(tunnelURL, repo string) error {
message.Debugf("git.addReadOnlyUserToRepo()")

// Add the readonly user to the repo
Expand All @@ -115,15 +115,15 @@ func (g *Git) addReadOnlyUserToRepo(tunnelUrl, repo string) error {
}

// Send API request to add a user as a read-only collaborator to a repo
addColabEndpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/collaborators/%s", tunnelUrl, g.Server.PushUsername, repo, g.Server.PullUsername)
addColabEndpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/collaborators/%s", tunnelURL, g.Server.PushUsername, repo, g.Server.PullUsername)
addColabRequest, _ := netHttp.NewRequest("PUT", addColabEndpoint, bytes.NewBuffer(addColabData))
out, err := g.DoHttpThings(addColabRequest, g.Server.PushUsername, g.Server.PushPassword)
out, err := g.DoHTTPThings(addColabRequest, g.Server.PushUsername, g.Server.PushPassword)
message.Debugf("PUT %s:\n%s", addColabEndpoint, string(out))
return err
}

// Add http request boilerplate and perform the request, checking for a successful response
func (g *Git) DoHttpThings(request *netHttp.Request, username, secret string) ([]byte, error) {
// DoHTTPThings adds http request boilerplate and perform the request, checking for a successful response
func (g *Git) DoHTTPThings(request *netHttp.Request, username, secret string) ([]byte, error) {
message.Debugf("git.DoHttpThings()")

// Prep the request with boilerplate
Expand Down
3 changes: 2 additions & 1 deletion src/internal/packager/git/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// For further explanation: https://regex101.com/r/zq64q4/1
var gitURLRegex = regexp.MustCompile(`^(?P<proto>[a-z]+:\/\/)(?P<hostPath>.+?)\/(?P<repo>[\w\-\.]+?)(?P<git>\.git)?(?P<atRef>@(?P<ref>[\w\-\.]+))?$`)

// MutateGitURlsInText Changes the giturl hostname to use the repository Zarf is configured to use
// MutateGitUrlsInText changes the giturl hostname to use the repository Zarf is configured to use.
func (g *Git) MutateGitUrlsInText(text string) string {
extractPathRegex := regexp.MustCompilePOSIX(`https?://[^/]+/(.*\.git)`)
output := extractPathRegex.ReplaceAllStringFunc(text, func(match string) string {
Expand All @@ -29,6 +29,7 @@ func (g *Git) MutateGitUrlsInText(text string) string {
return output
}

// TransformURLtoRepoName takes a git url and returns a zarf-compatible repo name
func (g *Git) TransformURLtoRepoName(url string) (string, error) {
matches := gitURLRegex.FindStringSubmatch(url)
idx := gitURLRegex.SubexpIndex
Expand Down
6 changes: 3 additions & 3 deletions src/internal/packager/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// InstallOrUpgradeChart performs a helm install of the given chart
func (h *Helm) InstallOrUpgradeChart() (types.ConnectStrings, string, error) {
fromMessage := h.Chart.Url
fromMessage := h.Chart.URL
if fromMessage == "" {
fromMessage = "Zarf-generated helm chart"
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func (h *Helm) InstallOrUpgradeChart() (types.ConnectStrings, string, error) {
return nil, "", fmt.Errorf("unable to initialize the K8s client: %w", err)
}

postRender, err := h.NewRenderer()
postRender, err := h.newRenderer()
if err != nil {
return nil, "", fmt.Errorf("unable to create helm renderer: %w", err)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (h *Helm) GenerateChart(manifest types.ZarfManifest) (types.ConnectStrings,

// Generate the struct to pass to InstallOrUpgradeChart()
h.Chart = types.ZarfChart{
Name: tmpChart.Metadata.Name,
Name: tmpChart.Metadata.Name,
// Preserve the zarf prefix for chart names to match v0.22.x and earlier behavior
ReleaseName: fmt.Sprintf("zarf-%s", sha1ReleaseName),
Version: tmpChart.Metadata.Version,
Expand Down
2 changes: 2 additions & 0 deletions src/internal/packager/helm/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"helm.sh/helm/v3/pkg/action"
)

// Destroy removes ZarfInitPackage charts from the cluster and optionally all Zarf-installed charts
func (h *Helm) Destroy(purgeAllZarfInstallations bool) {
spinner := message.NewProgressSpinner("Removing Zarf-installed charts")
defer spinner.Stop()
Expand Down Expand Up @@ -59,6 +60,7 @@ func (h *Helm) Destroy(purgeAllZarfInstallations bool) {
spinner.Success()
}

// RemoveChart removes a chart from the cluster
func (h *Helm) RemoveChart(namespace string, name string, spinner *message.Spinner) error {
// Establish a new actionConfig for the namespace
_ = h.createActionConfig(namespace, spinner)
Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type renderer struct {
values template.Values
}

func (h *Helm) NewRenderer() (*renderer, error) {
func (h *Helm) newRenderer() (*renderer, error) {
message.Debugf("helm.NewRenderer()")

valueTemplate, err := template.Generate(h.Cfg)
Expand Down
6 changes: 3 additions & 3 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (h *Helm) DownloadChartFromGit(destination string) string {
// Get the git repo
gitCfg := git.NewWithSpinner(h.Cfg.State.GitServer, spinner)

tempPath := gitCfg.DownloadRepoToTemp(h.Chart.Url)
tempPath := gitCfg.DownloadRepoToTemp(h.Chart.URL)
defer os.RemoveAll(tempPath)
gitCfg.GitPath = tempPath

Expand Down Expand Up @@ -82,7 +82,7 @@ func (h *Helm) DownloadChartFromGit(destination string) string {

// DownloadPublishedChart loads a specific chart version from a remote repo
func (h *Helm) DownloadPublishedChart(destination string) {
spinner := message.NewProgressSpinner("Processing helm chart %s:%s from repo %s", h.Chart.Name, h.Chart.Version, h.Chart.Url)
spinner := message.NewProgressSpinner("Processing helm chart %s:%s from repo %s", h.Chart.Name, h.Chart.Version, h.Chart.URL)
defer spinner.Stop()

// Set up the helm pull config
Expand All @@ -99,7 +99,7 @@ func (h *Helm) DownloadPublishedChart(destination string) {
// @todo: process OCI-based charts

// Perform simple chart download
chartURL, err := repo.FindChartInRepoURL(h.Chart.Url, h.Chart.Name, h.Chart.Version, pull.CertFile, pull.KeyFile, pull.CaFile, getter.All(pull.Settings))
chartURL, err := repo.FindChartInRepoURL(h.Chart.URL, h.Chart.Name, h.Chart.Version, pull.CertFile, pull.KeyFile, pull.CaFile, getter.All(pull.Settings))
if err != nil {
spinner.Fatalf(err, "Unable to pull the helm chart")
}
Expand Down
28 changes: 16 additions & 12 deletions src/internal/packager/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,32 @@ import (
func (i *ImgConfig) PushToZarfRegistry() error {
message.Debugf("images.PushToZarfRegistry(%#v)", i)

registryURL := ""
var (
err error
tunnel *cluster.Tunnel
registryURL string
target string
)

if i.RegInfo.InternalRegistry {
// Establish a registry tunnel to send the images to the zarf registry
tunnel, err := cluster.NewZarfTunnel()
if err != nil {
if tunnel, err = cluster.NewZarfTunnel(); err != nil {
return err
}
tunnel.Connect(cluster.ZarfRegistry, false)
defer tunnel.Close()

registryURL = tunnel.Endpoint()
target = cluster.ZarfRegistry
} else if cluster.IsServiceURL(i.RegInfo.Address) {
// If this is a serviceURL, create a port-forward tunnel to that resource
if tunnel, err := cluster.NewTunnelFromServiceURL(i.RegInfo.Address); err != nil {
if tunnel, err = cluster.NewTunnelFromServiceURL(i.RegInfo.Address); err != nil {
return err
} else {
tunnel.Connect("", false)
defer tunnel.Close()
registryURL = tunnel.Endpoint()
}
}

if tunnel != nil {
tunnel.Connect(target, false)
defer tunnel.Close()
registryURL = tunnel.Endpoint()
}

spinner := message.NewProgressSpinner("Storing images in the zarf registry")
defer spinner.Stop()

Expand Down
6 changes: 1 addition & 5 deletions src/internal/packager/sbom/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,5 @@ func OutputSBOMFiles(tmp types.TempPaths, outputDir string, packageName string)
return err
}

if err := utils.CreatePathAndCopy(tmp.Sboms, packagePath); err != nil {
return err
}

return nil
return utils.CreatePathAndCopy(tmp.Sboms, packagePath)
}
2 changes: 1 addition & 1 deletion src/internal/packager/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func validateChart(chart types.ZarfChart) error {
}

// Must only have a url or localPath
count := oneIfNotEmpty(chart.Url) + oneIfNotEmpty(chart.LocalPath)
count := oneIfNotEmpty(chart.URL) + oneIfNotEmpty(chart.LocalPath)
if count != 1 {
return fmt.Errorf(lang.PkgValidateErrChartUrlOrPath, chart.Name)
}
Expand Down
1 change: 1 addition & 0 deletions src/pkg/k8s/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
)

// List of supported distros via distro detection
const (
DistroIsUnknown = "unknown"
DistroIsK3s = "k3s"
Expand Down
1 change: 1 addition & 0 deletions src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pterm/pterm"
)

// LogLevel is the level of logging to display.
type LogLevel int

const (
Expand Down
Loading

0 comments on commit 0780bb7

Please sign in to comment.