Skip to content

Commit

Permalink
Send repo folder to CMP instead of just app folder
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Luz Almeida <leonardo_almeida@intuit.com>
  • Loading branch information
leoluz committed Mar 10, 2022
1 parent 475b8e6 commit 8880410
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 88 deletions.
135 changes: 89 additions & 46 deletions cmpserver/apiclient/plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions cmpserver/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,13 @@ func (s *Service) GenerateManifest(stream apiclient.ConfigManagementPluginServic
}
}()

env, err := cmp.ReceiveApplicationStream(stream.Context(), stream, workDir)
metadata, err := cmp.ReceiveRepoStream(stream.Context(), stream, workDir)
if err != nil {
return fmt.Errorf("generate manifest error receiving stream: %s", err)
}

response, err := s.generateManifest(stream.Context(), workDir, env)
appPath := filepath.Clean(filepath.Join(workDir, metadata.AppRelPath))
response, err := s.generateManifest(stream.Context(), appPath, metadata.GetEnv())
if err != nil {
return fmt.Errorf("error generating manifests: %s", err)
}
Expand All @@ -158,7 +159,7 @@ func (s *Service) GenerateManifest(stream apiclient.ConfigManagementPluginServic
}

// generateManifest runs generate command from plugin config file and returns generated manifest files
func (s *Service) generateManifest(ctx context.Context, workDir string, envEntries []*apiclient.EnvEntry) (*apiclient.ManifestResponse, error) {
func (s *Service) generateManifest(ctx context.Context, appDir string, envEntries []*apiclient.EnvEntry) (*apiclient.ManifestResponse, error) {
bufferedCtx, cancel := buffered_context.WithEarlierDeadline(ctx, cmpTimeoutBuffer)
defer cancel()

Expand All @@ -172,13 +173,13 @@ func (s *Service) generateManifest(ctx context.Context, workDir string, envEntri

env := append(os.Environ(), environ(envEntries)...)
if len(config.Spec.Init.Command) > 0 {
_, err := runCommand(bufferedCtx, config.Spec.Init, workDir, env)
_, err := runCommand(bufferedCtx, config.Spec.Init, appDir, env)
if err != nil {
return &apiclient.ManifestResponse{}, err
}
}

out, err := runCommand(bufferedCtx, config.Spec.Generate, workDir, env)
out, err := runCommand(bufferedCtx, config.Spec.Generate, appDir, env)
if err != nil {
return &apiclient.ManifestResponse{}, err
}
Expand Down Expand Up @@ -214,7 +215,7 @@ func (s *Service) MatchRepository(stream apiclient.ConfigManagementPluginService
}
}()

_, err = cmp.ReceiveApplicationStream(bufferedCtx, stream, workdir)
_, err = cmp.ReceiveRepoStream(bufferedCtx, stream, workdir)
if err != nil {
return fmt.Errorf("match repository error receiving stream: %s", err)
}
Expand Down
17 changes: 10 additions & 7 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,12 @@ func (s *Service) GenerateManifest(ctx context.Context, q *apiclient.ManifestReq
err = s.runRepoOperation(ctx, q.Revision, q.Repo, q.ApplicationSource, q.VerifySignature, cacheFn, operation, settings)

if tarConcluded && res == nil {
res = <-promise.responseCh
select {
case resp := <-promise.responseCh:
res = resp
case err := <-promise.errCh:
return nil, err
}
}

return res, err
Expand Down Expand Up @@ -473,7 +478,6 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,

}
ch.errCh <- err
return
}
// Otherwise, no error occurred, so ensure the manifest generation error data in the cache entry is reset before we cache the value
manifestGenCacheEntry := cache.CachedManifestResponse{
Expand All @@ -490,7 +494,6 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,
log.Warnf("manifest cache set error %s/%s: %v", q.ApplicationSource.String(), cacheKey, err)
}
ch.responseCh <- manifestGenCacheEntry.ManifestResponse
return
}

// getManifestCacheEntry returns false if the 'generate manifests' operation should be run by runRepoOperation, e.g.:
Expand Down Expand Up @@ -1322,7 +1325,7 @@ func getPluginEnvs(envVars *v1alpha1.Env, q *apiclient.ManifestRequest, creds gi

func runConfigManagementPluginSidecars(ctx context.Context, appPath, repoPath string, envVars *v1alpha1.Env, q *apiclient.ManifestRequest, creds git.Creds, tarDoneCh chan<- bool) ([]*unstructured.Unstructured, error) {
// detect config management plugin server (sidecar)
conn, cmpClient, err := discovery.DetectConfigManagementPlugin(ctx, appPath)
conn, cmpClient, err := discovery.DetectConfigManagementPlugin(ctx, repoPath)
if err != nil {
return nil, err
}
Expand All @@ -1333,7 +1336,7 @@ func runConfigManagementPluginSidecars(ctx context.Context, appPath, repoPath st
if err != nil {
return nil, err
}
cmpManifests, err := generateManifestsCMP(ctx, appPath, env, cmpClient, tarDoneCh)
cmpManifests, err := generateManifestsCMP(ctx, appPath, repoPath, env, cmpClient, tarDoneCh)
if err != nil {
return nil, fmt.Errorf("error generating manifests in cmp: %s", err)
}
Expand All @@ -1351,15 +1354,15 @@ func runConfigManagementPluginSidecars(ctx context.Context, appPath, repoPath st
// generateManifestsCMP will send the appPath files to the cmp-server over a gRPC stream.
// The cmp-server will generate the manifests. Returns a response object with the generated
// manifests.
func generateManifestsCMP(ctx context.Context, appPath string, env []string, cmpClient pluginclient.ConfigManagementPluginServiceClient, tarDoneCh chan<- bool) (*pluginclient.ManifestResponse, error) {
func generateManifestsCMP(ctx context.Context, appPath, repoPath string, env []string, cmpClient pluginclient.ConfigManagementPluginServiceClient, tarDoneCh chan<- bool) (*pluginclient.ManifestResponse, error) {
generateManifestStream, err := cmpClient.GenerateManifest(ctx, grpc_retry.Disable())
if err != nil {
return nil, fmt.Errorf("error getting generateManifestStream: %s", err)
}
opts := []cmp.SenderOption{
cmp.WithTarDoneChan(tarDoneCh),
}
err = cmp.SendApplicationStream(generateManifestStream.Context(), appPath, generateManifestStream, env, opts...)
err = cmp.SendRepoStream(generateManifestStream.Context(), appPath, repoPath, generateManifestStream, env, opts...)
if err != nil {
return nil, fmt.Errorf("error sending file to cmp-server: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion test/cmp/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ spec:
generate:
command: [sh, -c, 'kustomize build']
discover:
fileName: "kustomization.yaml"
find:
glob: "**/kustomization.yaml"
allowConcurrency: true
lockRepo: false
Loading

0 comments on commit 8880410

Please sign in to comment.