// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
//     http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 utils

import (
	"os"
	"path/filepath"
	"strings"

	appsV1 "k8s.io/api/apps/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
)

// NamespacedName returns the namespaced name for k8s objects
func NamespacedName(obj v1.Object) types.NamespacedName {
	return types.NamespacedName{
		Namespace: obj.GetNamespace(),
		Name:      obj.GetName(),
	}
}

func GetEnvValueForKeyFromDaemonSet(key string, ds *appsV1.DaemonSet) string {
	envVar := ds.Spec.Template.Spec.Containers[0].Env
	for _, env := range envVar {
		if env.Name == key {
			return env.Value
		}
	}
	return ""
}

func GetProjectRoot() string {
	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	projectRoot := strings.SplitAfter(dir, "amazon-vpc-cni-k8s")[0]

	if dir == projectRoot {
		// in prow tests, the repository name is "vpc-cni"
		projectRoot = strings.SplitAfter(dir, "vpc-cni")[0]
	}
	return projectRoot
}
