Skip to content

Commit

Permalink
Add /version endpoint (#9296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-laj authored Jun 15, 2020
1 parent 9312a04 commit 832593a
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 8 deletions.
40 changes: 40 additions & 0 deletions airflow/api_connexion/endpoints/version_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

import logging
from typing import NamedTuple, Optional

import airflow
from airflow.api_connexion.schemas.version_schema import version_info_schema
from airflow.utils.platform import get_airflow_git_version

log = logging.getLogger(__name__)


class VersionInfo(NamedTuple):
"""Version information"""
version: str
git_version: Optional[str]


def get_version():
"""Get version information"""
airflow_version = airflow.__version__

git_version = get_airflow_git_version()
version_info = VersionInfo(version=airflow_version, git_version=git_version)
return version_info_schema.dump(version_info)
25 changes: 25 additions & 0 deletions airflow/api_connexion/openapi/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,19 @@ paths:
schema:
type: string

/version:
get:
summary: Get version information
operationId: airflow.api_connexion.endpoints.version_endpoint.get_version
tags: [Monitoring]
responses:
'200':
description: Return current configuration.
content:
application/json:
schema:
$ref: '#/components/schemas/VersionInfo'


components:
# Reusable schemas (data models)
Expand Down Expand Up @@ -1733,6 +1746,18 @@ components:
items:
$ref: '#/components/schemas/ConfigSection'

VersionInfo:
type: object
description: Version information.
properties:
version:
type: string
description: The version of Airflow
git_version:
type: string
description: The git version (including git commit hash)
nullable: true

# From
ClearTaskInstance:
type: object
Expand Down
27 changes: 27 additions & 0 deletions airflow/api_connexion/schemas/version_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

from marshmallow import Schema, fields


class VersionInfoSchema(Schema):
"""Version information schema"""
version = fields.String(dump_only=True)
git_version = fields.String(dump_only=True)


version_info_schema = VersionInfoSchema(strict=True)
15 changes: 15 additions & 0 deletions airflow/utils/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
"""
Platform and system specific function.
"""
import logging
import os
import pkgutil
import sys

log = logging.getLogger(__name__)


def is_tty():
"""
Expand All @@ -45,3 +49,14 @@ def is_terminal_support_colors() -> bool:
if term in ("xterm", "linux") or "color" in term:
return True
return False


def get_airflow_git_version():
"""Returns the git commit hash representing the current version of the application."""
git_version = None
try:
git_version = str(pkgutil.get_data('airflow', 'git_version'), encoding="UTF-8")
except Exception as e: # pylint: disable=broad-except
log.error(e)

return git_version
10 changes: 2 additions & 8 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import logging
import math
import os
import pkgutil
import socket
import traceback
from collections import defaultdict
Expand Down Expand Up @@ -68,6 +67,7 @@
from airflow.utils import timezone
from airflow.utils.dates import infer_time_unit, scale_time_units
from airflow.utils.helpers import alchemy_to_dict, render_log_filename
from airflow.utils.platform import get_airflow_git_version
from airflow.utils.session import create_session, provide_session
from airflow.utils.state import State
from airflow.www import utils as wwwutils
Expand Down Expand Up @@ -2095,13 +2095,7 @@ def version(self):
airflow_version = None
logging.error(e)

# Get the Git repo and git hash
git_version = None
try:
git_version = str(pkgutil.get_data('airflow', 'git_version'), encoding="UTF-8")
except Exception as e:
logging.error(e)

git_version = get_airflow_git_version()
# Render information
title = "Version Info"
return self.render_template(
Expand Down
43 changes: 43 additions & 0 deletions tests/api_connexion/endpoints/test_version_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
import unittest
from unittest import mock

from airflow.www import app


class TestGetHealthTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
cls.app = app.create_app(testing=True) # type:ignore

def setUp(self) -> None:
self.client = self.app.test_client() # type:ignore

@mock.patch(
"airflow.api_connexion.endpoints.version_endpoint.airflow.__version__", "MOCK_VERSION"
)
@mock.patch(
"airflow.api_connexion.endpoints.version_endpoint.get_airflow_git_version", return_value="GIT_COMMIT"
)
def test_should_response_200(self, mock_get_airflow_get_commit):
response = self.client.get("/api/v1/version")

self.assertEqual(200, response.status_code)
self.assertEqual({'git_version': 'GIT_COMMIT', 'version': 'MOCK_VERSION'}, response.json)
mock_get_airflow_get_commit.assert_called_once_with()
37 changes: 37 additions & 0 deletions tests/api_connexion/schemas/test_version_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

import unittest

from parameterized import parameterized

from airflow.api_connexion.endpoints.version_endpoint import VersionInfo
from airflow.api_connexion.schemas.version_schema import version_info_schema


class TestVersionInfoSchema(unittest.TestCase):

@parameterized.expand([
("GIT_COMMIT", ),
(None, ),
])
def test_serialize(self, git_commit):
version_info = VersionInfo("VERSION", git_commit)
current_data = version_info_schema.dump(version_info).data

expected_result = {'version': 'VERSION', 'git_version': git_commit}
self.assertEqual(expected_result, current_data)

0 comments on commit 832593a

Please sign in to comment.