From 03c307ef36d297836ebe4cc7e2aca0453171774e Mon Sep 17 00:00:00 2001
From: Jessica Yuen <im.jessicayuen@gmail.com>
Date: Thu, 22 Feb 2018 14:17:56 -0800
Subject: [PATCH] Add warning for running deprecated ks app against ks >= 0.9.0

This is a temporary piece of code that should be removed after 1.0.0.

This warning exists to tell users that are using ks < 0.9.0 with the
dated environments model (contains the 'spec.json' file), to migrate to
the newer 'app.yaml' model.

Signed-off-by: Jessica Yuen <im.jessicayuen@gmail.com>
---
 cmd/apply.go            | 16 ++++++++++++++++
 metadata/environment.go | 28 ++++++++++++++++++++++++++++
 metadata/interface.go   |  2 ++
 3 files changed, 46 insertions(+)

diff --git a/cmd/apply.go b/cmd/apply.go
index 86504b03..1db95589 100644
--- a/cmd/apply.go
+++ b/cmd/apply.go
@@ -22,6 +22,7 @@ import (
 	"github.com/spf13/cobra"
 
 	"github.com/ksonnet/ksonnet/client"
+	"github.com/ksonnet/ksonnet/metadata"
 	"github.com/ksonnet/ksonnet/pkg/kubecfg"
 )
 
@@ -124,6 +125,21 @@ var applyCmd = &cobra.Command{
 			return err
 		}
 
+		// TODO remove after 1.0.0
+		// We are ensuring here that users aren't running a deprecated ksonnet
+		// app structure against a newer version of ks.
+		appDir, err := os.Getwd()
+		if err != nil {
+			return err
+		}
+		manager, err := metadata.Find(appDir)
+		if err != nil {
+			return err
+		}
+		if err := manager.ErrorOnSpecFile(); err != nil {
+			return err
+		}
+
 		return c.Run(objs, cwd)
 	},
 	Long: `
diff --git a/metadata/environment.go b/metadata/environment.go
index 06aea484..d23d7b71 100644
--- a/metadata/environment.go
+++ b/metadata/environment.go
@@ -385,6 +385,34 @@ func (m *manager) EnvPaths(env string) (libPath, mainPath, paramsPath string, er
 	return
 }
 
+func (m *manager) ErrorOnSpecFile() error {
+	return afero.Walk(m.appFS, m.environmentsPath, func(p string, f os.FileInfo, err error) error {
+		if err != nil {
+			log.Debugf("Failed to walk path %s", p)
+			return err
+		}
+		isDir, err := afero.IsDir(m.appFS, p)
+		if err != nil {
+			log.Debugf("Failed to check whether the path at %s is a directory", p)
+			return err
+		}
+		if isDir {
+			specPath := filepath.Join(p, "spec.json")
+			specFileExists, err := afero.Exists(m.appFS, specPath)
+			if err != nil {
+				log.Debugf("Failed to check whether spec.json exists")
+				return err
+			}
+			if specFileExists {
+				// TODO, we should point users to a tutorial.
+				return fmt.Errorf("Environment's directory contains a dated model containing the 'spec.json' file. Please migrate to the new model by adding environments data to app.yaml")
+			}
+		}
+
+		return nil
+	})
+}
+
 func (m *manager) tryMvEnvDir(dirPathOld, dirPathNew string) error {
 	// first ensure none of these paths exists in the new directory
 	for _, p := range envPaths {
diff --git a/metadata/interface.go b/metadata/interface.go
index 88407d69..d60dcd82 100644
--- a/metadata/interface.go
+++ b/metadata/interface.go
@@ -63,6 +63,8 @@ type Manager interface {
 	GetEnvironments() (app.EnvironmentSpecs, error)
 	GetEnvironment(name string) (*app.EnvironmentSpec, error)
 	SetEnvironment(name, desiredName string) error
+	// ErrorOnSpecFile is a temporary API to inform < 0.9.0 ks users of environment directory changes.
+	ErrorOnSpecFile() error
 
 	// Spec API.
 	AppSpec() (*app.Spec, error)
-- 
GitLab