-
Notifications
You must be signed in to change notification settings - Fork 11
refactor: image-manifest command uses service manager client factory #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bernielomax
merged 3 commits into
main
from
bernielomax/refactor/image-manifest-svc-mgr-factory
Jul 29, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,108 @@ | ||
package helm | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
"strings" | ||
|
||
goHelm "github.com/mittwald/go-helm-client" | ||
"helm.sh/helm/v3/pkg/repo" | ||
appsv1 "k8s.io/api/apps/v1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/kubectl/pkg/scheme" | ||
|
||
"github.com/airbytehq/abctl/internal/common" | ||
) | ||
|
||
func FindImagesFromChart(client goHelm.Client, valuesYaml, chartName, chartVersion string) ([]string, error) { | ||
err := client.AddOrUpdateChartRepo(repo.Entry{ | ||
Name: common.AirbyteRepoName, | ||
URL: common.AirbyteRepoURL, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rel, err := client.InstallChart(context.TODO(), &goHelm.ChartSpec{ | ||
ChartName: chartName, | ||
GenerateName: true, | ||
ValuesYaml: valuesYaml, | ||
Version: chartVersion, | ||
DryRun: true, | ||
}, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
images := findAllImages(rel.Manifest) | ||
return images, nil | ||
} | ||
|
||
// findAllImages walks through the Helm chart, looking for container images in k8s PodSpecs. | ||
// It also looks for env vars in the airbyte-env config map that end with "_IMAGE". | ||
// It returns a unique, sorted list of images found. | ||
func findAllImages(chartYaml string) []string { | ||
objs := decodeK8sResources(chartYaml) | ||
imageSet := common.Set[string]{} | ||
|
||
for _, obj := range objs { | ||
|
||
var podSpec *corev1.PodSpec | ||
switch z := obj.(type) { | ||
case *corev1.ConfigMap: | ||
if strings.HasSuffix(z.Name, "airbyte-env") { | ||
for k, v := range z.Data { | ||
if strings.HasSuffix(k, "_IMAGE") { | ||
imageSet.Add(v) | ||
} | ||
} | ||
} | ||
continue | ||
case *corev1.Pod: | ||
podSpec = &z.Spec | ||
case *batchv1.Job: | ||
podSpec = &z.Spec.Template.Spec | ||
case *appsv1.Deployment: | ||
podSpec = &z.Spec.Template.Spec | ||
case *appsv1.StatefulSet: | ||
podSpec = &z.Spec.Template.Spec | ||
default: | ||
continue | ||
} | ||
|
||
for _, c := range podSpec.InitContainers { | ||
imageSet.Add(c.Image) | ||
} | ||
for _, c := range podSpec.Containers { | ||
imageSet.Add(c.Image) | ||
} | ||
} | ||
|
||
var out []string | ||
for _, k := range imageSet.Items() { | ||
if k != "" { | ||
out = append(out, k) | ||
} | ||
} | ||
slices.Sort(out) | ||
|
||
return out | ||
} | ||
|
||
func decodeK8sResources(renderedYaml string) []runtime.Object { | ||
out := []runtime.Object{} | ||
chunks := strings.Split(renderedYaml, "---") | ||
for _, chunk := range chunks { | ||
if len(chunk) == 0 { | ||
continue | ||
} | ||
obj, _, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(chunk), nil, nil) | ||
if err != nil { | ||
continue | ||
} | ||
out = append(out, obj) | ||
} | ||
return out | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only change from the original function (apart from Helm client dependency injection) is replacing local template rendering (
TemplateChart
) withInstallChart
in dry-run mode.TemplateChart
uses local rendering, which does not talk to Kubernetes and can clear or ignore the client’s discovered capabilities, requiring a throwaway Helm client to avoid issues.InstallChart
(withDryRun: true
) connects to the cluster, preserves discovered capabilities, and uses them when rendering. Something not possible with local rendering.