Skip to content
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

Adds implementation of Helm OCI refererrs endpoint. #7131

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions chart/kubeapps/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Bitnami package for Kubeapps

Kubeapps is a web-based UI for launching and managing applications on Kubernetes. It allows users to deploy trusted applications and operators to control users access to the cluster.
Expand Down Expand Up @@ -1144,4 +1143,4 @@ Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ func TestAddPackageRepository(t *testing.T) {
if tc.existingDockerSecret != nil {
secrets = append(secrets, tc.existingDockerSecret)
}
s := newServerWithSecretsAndRepos(t, secrets, nil, nil)
s := newServerWithSecretsAndRepos(t, secrets, nil)
if tc.testRepoServer != nil {
defer tc.testRepoServer.Close()
s.repoClientGetter = func(_ *appRepov1.AppRepository, _ *apiv1.Secret) (*http.Client, error) {
Expand Down Expand Up @@ -1194,22 +1194,12 @@ func TestGetPackageRepositoryDetail(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var unstructuredObjects []k8sruntime.Object
for _, obj := range []*appRepov1alpha1.AppRepository{repo1, repo2, repo3, repo4, repo5} {
repository := obj
if tc.repositoryCustomizer != nil {
repository = tc.repositoryCustomizer(obj)
}
unstructuredContent, _ := k8sruntime.DefaultUnstructuredConverter.ToUnstructured(repository)
unstructuredObjects = append(unstructuredObjects, &unstructured.Unstructured{Object: unstructuredContent})
}

var secrets []k8sruntime.Object
if tc.existingSecret != nil {
secrets = append(secrets, tc.existingSecret)
}

s := newServerWithSecretsAndRepos(t, secrets, unstructuredObjects, nil)
s := newServerWithSecretsAndRepos(t, secrets, []*appRepov1alpha1.AppRepository{repo1, repo2, repo3, repo4, repo5})

actualResponse, err := s.GetPackageRepositoryDetail(context.Background(), connect.NewRequest(tc.request))

Expand Down Expand Up @@ -1935,12 +1925,6 @@ func TestUpdatePackageRepository(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var unstructuredObjects []k8sruntime.Object
for _, obj := range repos {
unstructuredContent, _ := k8sruntime.DefaultUnstructuredConverter.ToUnstructured(obj)
unstructuredObjects = append(unstructuredObjects, &unstructured.Unstructured{Object: unstructuredContent})
}

var secrets []k8sruntime.Object
if tc.existingAuthSecret != nil {
secrets = append(secrets, tc.existingAuthSecret)
Expand All @@ -1949,7 +1933,7 @@ func TestUpdatePackageRepository(t *testing.T) {
secrets = append(secrets, tc.existingDockerSecret)
}

s := newServerWithSecretsAndRepos(t, secrets, unstructuredObjects, repos)
s := newServerWithSecretsAndRepos(t, secrets, repos)

request := tc.requestCustomizer(commonRequest())
response, err := s.UpdatePackageRepository(context.Background(), connect.NewRequest(request))
Expand Down Expand Up @@ -2067,13 +2051,7 @@ func TestDeletePackageRepository(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var unstructuredObjects []k8sruntime.Object
for _, obj := range repos {
unstructuredContent, _ := k8sruntime.DefaultUnstructuredConverter.ToUnstructured(obj)
unstructuredObjects = append(unstructuredObjects, &unstructured.Unstructured{Object: unstructuredContent})
}

s := newServerWithSecretsAndRepos(t, nil, unstructuredObjects, repos)
s := newServerWithSecretsAndRepos(t, nil, repos)

_, err := s.DeletePackageRepository(context.Background(), connect.NewRequest(tc.request))

Expand Down
66 changes: 65 additions & 1 deletion cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/bufbuild/connect-go"
imageSpecv1 "github.com/opencontainers/image-spec/specs-go/v1"
appRepov1 "github.com/vmware-tanzu/kubeapps/cmd/apprepository-controller/pkg/apis/apprepository/v1alpha1"
"github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/core"
corev1 "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1"
Expand Down Expand Up @@ -44,6 +45,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
log "k8s.io/klog/v2"
"oras.land/oras-go/v2/registry/remote"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -1268,5 +1270,67 @@ func (s *Server) DeletePackageRepository(ctx context.Context, request *connect.R
}

func (s *Server) GetAvailablePackageMetadatas(ctx context.Context, request *connect.Request[corev1.GetAvailablePackageMetadatasRequest]) (*connect.Response[corev1.GetAvailablePackageMetadatasResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, fmt.Errorf("Unimplemented"))
chartID := request.Msg.GetAvailablePackageRef().GetIdentifier()
repoNamespace := request.Msg.GetAvailablePackageRef().GetContext().GetNamespace()
repoName, chartName, err := pkgutils.SplitPackageIdentifier(chartID)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}

// TODO: Ignoring secrets for now, just demoing with public OCI repo
appRepo, _, _, _, err := s.getAppRepoAndRelatedSecrets(ctx, request.Header(), s.globalPackagingCluster, repoName, repoNamespace)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to fetch app repo %q from namespace %q: %v", repoName, repoNamespace, err))
}
if appRepo.Spec.Type != OCIRepoType {
return nil, connect.NewError(connect.CodeUnimplemented, fmt.Errorf("Unimplemented for non-OCI repositories"))
}

repoURL := appRepo.Spec.URL
repoURL = strings.TrimPrefix(repoURL, "oci://")
repoURL = strings.TrimPrefix(repoURL, "https://")
plainHTTP := false
if strings.HasPrefix(repoURL, "http://") {
plainHTTP = true
repoURL = strings.TrimPrefix(repoURL, "http://")
}

tag := path.Join(repoURL, chartName)
fmt.Printf("\ntag: %+v", tag)
repo, err := remote.NewRepository(tag)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to call NewRepository with %q: %v", tag, err))
}
repo.PlainHTTP = plainHTTP

// Create the ocispec.Descriptor, first need to get the manifest of the chart.
descriptor, _, err := repo.Manifests().FetchReference(ctx, request.Msg.GetPkgVersion())
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to fetch manifest for %v with ref %q: %v", repo, request.Msg.GetPkgVersion(), err))
}

referrers := []imageSpecv1.Descriptor{}
err = repo.Referrers(ctx, descriptor, "", func(r []imageSpecv1.Descriptor) error {
referrers = append(referrers, r...)
return nil
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to fetch referrers for %v with ref %q: %v", repo, request.Msg.GetPkgVersion(), err))
}

package_metadatas := []*corev1.PackageMetadata{}
for _, referrer := range referrers {
package_metadatas = append(package_metadatas, &corev1.PackageMetadata{
Name: referrer.Annotations["org.opencontainers.image.title"],
Description: referrer.Annotations["org.opencontainers.image.description"],
MediaType: referrer.MediaType,
ArtifactType: referrer.ArtifactType,
Digest: referrer.Digest.String(),
})
}

return connect.NewResponse(&corev1.GetAvailablePackageMetadatasResponse{
AvailablePackageRef: request.Msg.AvailablePackageRef,
PackageMetadata: package_metadatas,
}), nil
}
Loading
Loading