/*
Copyright 2018 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

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.
*/

package builder

import (
	"github.com/gravitational/gravity/lib/constants"
	"github.com/gravitational/gravity/lib/defaults"
	"github.com/gravitational/gravity/lib/schema"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/helm/pkg/proto/hapi/chart"
)

// generateManifest generates an application manifest for the provided Helm chart.
func generateManifest(chart *chart.Chart) (*schema.Manifest, error) {
	return &schema.Manifest{
		Header: schema.Header{
			TypeMeta: metav1.TypeMeta{
				Kind:       schema.KindApplication,
				APIVersion: schema.APIVersionV2,
			},
			Metadata: schema.Metadata{
				Name:            chart.Metadata.Name,
				ResourceVersion: chart.Metadata.Version,
				Description:     chart.Metadata.Description,
				Repository:      defaults.SystemAccountOrg,
				// Mark the application with a label so Gravity has an easy
				// way to detect this was built out of Helm chart.
				Labels: map[string]string{
					constants.HelmLabel:       "true",
					constants.AppVersionLabel: chart.Metadata.AppVersion,
				},
			},
		},
		Logo: chart.Metadata.Icon,
	}, nil
}
