maclof/kubernetes-client

Allow override of apiVersion

glennschmidt opened this issue · 6 comments

Different models require different apiVersion strings depending on the Kubernetes server version.

eg. CronJob needs batch/v2alpha1 for <=1.9 but batch/v1beta1 for 1.10+

If the library doesn't do this automatically, it should expose some way for the versions to be set by the user.

I completely agree, I think it should attempt to do it automatically but also allow overrides.

@glennschmidt this is now fixed, as much as i'd love to do the detection automatically for now I've just added a public setApiVersion() function on models, you can use this to change the api version before calling create/update.

How can i change the api version by find ?

$deploy = $client->daemonSets()->find([
            'labelSelector' => 'app=test'
        ]);

my k8s api is apps/v1 not extensions/v1beta1

@dongkaipo I can't find a way to do that unfortunately; it looks like apiVersion is mostly hard coded into the Model classes, as getApiVersion will instantiate a new Model (eg. for DaemonSetRepository it will instantiate DaemonSet) and then pull the apiVersion off that.

@michaelbutler can i add a method setApiVersion to abstract class Repository ?

<?php namespace Maclof\Kubernetes\Repositories;

use Closure;
use Maclof\Kubernetes\Models\Model;
use Maclof\Kubernetes\Models\DeleteOptions;
use Maclof\Kubernetes\Repositories\Utils\JSONStreamingParser;
use Maclof\Kubernetes\Repositories\Utils\JSONStreamingListener;

abstract class Repository
{
	/**
	 * The client.
	 *
	 * @var \Maclof\Kubernetes\Client
	 */
	protected $client;

	/**
	 * Include the namespace in the requests.
	 *
	 * @var boolean
	 */
	protected $namespace = true;

	/**
	 * The api version to use for requests.
	 *
	 * @var null
	 */
	protected $apiVersion;

	/**
	 * The label selector.
	 *
	 * @var array
	 */
	protected $labelSelector = [];

	/**
	 * The field selector.
	 *
	 * @var array
	 */
	protected $fieldSelector = [];


	/**
	 * The default class namespace of the repositories
	 * 
	 * @var string
	 */
	protected $modelClassNamespace = 'Maclof\Kubernetes\Models\\';

	/**
	 * The constructor.
	 *
	 * @param \Maclof\Kubernetes\Client $client
	 */
	public function __construct($client)
	{
		$this->client = $client;
	}

        public function setApiVersion($version){
                $this->apiVersion = $version;
        }